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.
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.
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.