Servelt Page doesn't Redirect to second Page using Servlet

mayu kobi :

I am a beginner of servlet jsp. I am creating a simple login form if the login successful page redirects to the second servlet along with the username. but it doesn't work it shows the error java.lang.IllegalArgumentException: Path second does not start with a "/" character what I tried so far I attached below.

Form

 <div class="row">
                <form method="POST" action="login">
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" id="uname" name="uname" placeholder="uname" class="form-control"> 
                </div>

                 <div class="form-group">
                    <label>Password</label>
                    <input type="password" id="pword" name="pword" placeholder="pword" class="form-control">

                </div>


                 <div class="form-group">


                     <input type="submit" value="submit" class="btn btn-success">

                </div>

                </form>


            </div>

Login Servlet Page

@WebServlet("/login")
public class login extends HttpServlet {
  @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {

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

         if(uname.equals("John") && pass.equals("123"))
         {
            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession(true); 
            session.putValue("username", uname);

            ServletContext context=getServletContext();
            RequestDispatcher rd=context.getRequestDispatcher("second");  

            rd.forward(request, response);  

         }



    }

Second Servlet Page

@WebServlet("/second")
public class second extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        PrintWriter out = response.getWriter();
         HttpSession session = request.getSession(true);
       String uname = (String)session.getValue("uname");
        out.println("User Name is " + uname);

    }
Sebastian I. :

There are some big differences between redirect and forward . This article will help you to understand better your problem.

Some things to keep in mind from the article :

FORWARD

1) Request and response objects will remain the same object after forwarding. Request-scope objects will be still available (this is why if you try to add the "/" you get the 405 status - it will try to forward you to the "/second" servlet but the only request overridden is GET)

REDIRECT

1) The request is redirected to a different resource

2) A new request is created

So instead of using rd.forward I would suggest you to use the sendRedirect() method from HttpServletResponse class.

response.sendRedirect(request.getContextPath() + "/second");

AND the correct way to get the session username attribute is this:

String uname = (String) session.getValue("username");

Otherwise it will try to look after uname key which is not set. The uname was only a value mapped to the username key ( session.putValue("username",uname);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=417134&siteId=1