0

After logging in, username is passed to the servlet containing following form, and is showing as the logged in user. But after submitting this form, I am not able to find a way to pass username to the next servlet as the form action happens on submission. So, RequestDispatcher is not useful. Can anybody help me as to how to pass username to next servlet after this form submission so that I can use username on every single page after login?

    pw.print("<Form action=BusSearch method=doGet>");
    pw.print("<input type=text name=from placeholder=From class=textbox><br>");                                 
    pw.print("<input type=text name=to placeholder=To class=textbox>");
    pw.print("<br>");
    pw.print("<input type=text name=DOJ placeholder=Date of Journey class=textbox</br>");
    pw.print("<button type=submit value=Search class=button>");
    pw.print("</Form>");
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Jeff
  • 1
  • 3
  • This code snippet does not seem to be relevant to your problem. Also, your question should be clearer. As it stands people will probably try to guess what your problem is. Maybe try to rephrase it? Have a look at this: http://stackoverflow.com/help/mcve – Mark Chorley Jul 14 '16 at 17:01

1 Answers1

0

Using PrintWriter is not a good idea anyway, to maintain user specific state you can use Sessions to use a session, first create a session using the HttpServletRequest method getSession() in BusSearch servlet :

HttpSession session = request.getSession();

Once the session is created, examine and set its properties using the provided methods. To set username for example you can do this :

String username = "Username";    
session.setAttribute("User", username);

Now you can retrieve username from the created session in all other servlets to do so :

HttpSession session = request.getSession();
String Username = (String) session.getAttribute("User");