Zayd Khan
Zayd Khan

Reputation: 152

How to pass parameter from on servlet to another?

I'm able to get parameters from the html file; but i'm not able to send those parameters to other Servlet..

What exactly i want to do is, that get the user name and password in one servlet and then send those values to another servlet for authentication and then redirect accordingly....

And i'm new to this I've tried many things blindly and not able to get the work done....

This is my 1st Servlet:

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
PrintWriter out=response.getWriter();
if(uname!=null && pass!=null){
    ServletContext sc=getServletContext();
    RequestDispatcher r=sc.getRequestDispatcher("/Authentication");
    response.sendRedirect("Authentication");
} else {
    out.println("Error");
}

And this is my 2nd Servlet:

public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        String uname=(String)request.getAttribute("uname");
        String pass=(String)request.getAttribute("pass");
        int p=pass.hashCode();
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aed","root","passwordmysql");
        Statement st=con.createStatement();
        ResultSet rs;
        rs=st.executeQuery("select * from logids where userName='" + uname + "' and pass='" + p +"'");
        if(rs.next()){
            response.sendRedirect("home.html"); 
        }else{
            response.sendRedirect("Error.html");
        }
        }
        catch(SQLException i){
            i.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I'm new at this and i'm pretty sure i've done mistakes.....

Upvotes: 1

Views: 3997

Answers (2)

theRoot
theRoot

Reputation: 569

Refer the docs : link

forwarding servlet lets you to forward the current servlet context objects(their parameters are forwarded username , password)

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    response.setContentType("text/html");
    String uname=request.getParameter("uname");
    String pass=request.getParameter("pass");
    PrintWriter out=response.getWriter();
    if(uname!=null && pass!=null){
        ServletContext sc=getServletContext();
        RequestDispatcher r=sc.getRequestDispatcher("/Authentication");
        response.forward("Authentication");
    } else {
        out.println("Error");
    }

second servlet

public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        String uname=(String)request.getParameter("uname");
        String pass=(String)request.getParameter("pass");
        int p=pass.hashCode();
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aed","root","passwordmysql");
        Statement st=con.createStatement();
        ResultSet rs;
        rs=st.executeQuery("select * from logids where userName='" + uname + "' and pass='" + p +"'");
        if(rs.next()){
            response.sendRedirect("home.html"); 
        }else{
            response.sendRedirect("Error.html");
        }
        }
        catch(SQLException i){
            i.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

NOTE : place your data access codes seperately

Upvotes: 0

SMA
SMA

Reputation: 37023

Once you get the parameters from form, you would need to set the attribute at session level to be available to other servlet. So after following statement:

String uname=request.getParameter("uname");
String pass=request.getParameter("pass");

Add the following:

 request.getSession().setAttribute("uname", uname);
 request.getSession().setAttribute("pass", pass);

And then in servlet2, access it like:

String uname= (String)request.getSession().getAttribute("uname");
String pass= (String)request.getSession().getAttribute("pass");

Upvotes: 1

Related Questions