0

I am new to programming and i have written two pieces of code to learn urlrewriting in servlet:

My html form is :

<form action="loginhidden" method="get">
    Login ID:<input name="login" ><br>
    Password:<input name="pass" type="password"><br>
             <input type="submit" >
</form>

My web.xml file is :

<web-app>
        <servlet>
        <servlet-name>loginhidden</servlet-name>
        <servlet-class>loginhidden</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>loginhidden</servlet-name>
        <url-pattern>/loginhidden</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>loginhidden1_name</servlet-name>
        <servlet-class>loginhidden1_name</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>loginhidden1_name</servlet-name>
        <url-pattern>/loginhidden1_name/*</url-pattern>
    </servlet-mapping>
</web-app>

The pieces of code are as follows:

1.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

    public class loginhidden extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse res)throws 
        ServletException, IOException                                          

           {
               res.setContentType("text/html");
               PrintWriter out=res.getWriter();
               String login= req.getParameter("login");
               String pass=req.getParameter("pass");
               if(pass.equals("admin"))
                   {
                   out.println(login);
                   out.println(pass);
                   out.println("<html><head><form action=loginhidden1_name?
                   mylogin="+login+">");

                   out.println("Your Name:<input type=text name=myname><br>");
                   out.println("<input type=submit>");
                   out.println("</body></head></html>");
                  }

           }


         }

2.

 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;

   public class loginhidden1_name extends HttpServlet{
   @Override
       public void doGet(HttpServletRequest req, HttpServletResponse res )throws   
       ServletException, IOException 

       {
           res.setContentType("text/html");
           PrintWriter out=res.getWriter();
           out.println(req.getParameter("mylogin"));
           out.println(req.getParameter("myname"));
       }
     }

I am able to get the value of name in my second servlet(loginhidden1_name) but i am not able to get the value of login id("mylogin") through urlrewriting.I am getting null value for it.Please Help.

Thanks a lot in Advance.

gallactico
  • 13
  • 2
  • What do you want to happen? Do you want control to transfer to the second servlet under in some cases? – BillRobertson42 Feb 18 '14 at 03:08
  • I want to get the value of loginid of user from the html page(form) .. get it printed in the first servlet and then pass the value through url rewriting to the second servlet and get it printed again. Thanks Bill. – gallactico Feb 19 '14 at 02:50

2 Answers2

0

If you're just looking to transfer control from one servlet to another, it's a simple matter of forwarding the request to the other servlet. A "forward" in this case does not go back to the client.

In your original servlet, at the end, you'll want to get a RequestDispatcher, and forward to the new URL.

e.g.

getServletContext().getRequestDispatcher("/modified/url").forward(request, response);

The thread of control will transfer to the other servlet. IIRC, you will still finish out the method call in the first servlet. i.e. it doesn't return from your method and then call the other servlet.

You can take advantage of this if you need post processing of the request for some reason. Although a ServletFitler would be a more appropriate way to handle this case.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
0

You cannot can use URL rewriting in a form action. Any parameters after ? will be dropped by the browser. Instead you can add the login as a hidden form field in your second form:

...
out.println("<input type=hidden name=\"mylogin\" value=\""+login+"\">");
...

This will be passed through to your second Servlet in the same way as the other fields.

See submitting a GET form with query string params and hidden params disappear

Community
  • 1
  • 1
Si Kelly
  • 703
  • 3
  • 9