Reputation: 85
I am making a small program where user's name will be used all over the pages where he navigates. I have written following code.
JSP file:
<form action="CookieServletOne" method="post">
User Name:<input type="text" name="username">
<input type="submit" value="Go">
</form>
Servlet One (under post method):
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
String username=request.getParameter("username");
pw.println("Welcome " +username);
Cookie ck=new Cookie("un",username);
response.addCookie(ck);
pw.print("<form action='CookieServletTwo'>");
pw.print("<input type='submit' value='go'>");
pw.print("</form>");
pw.close();
Servlet2:
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Cookie[] ck=request.getCookies();
pw.write("Hello" +ck[0].getValue());
I want to take the value which is written in text box to the Servlet2 by using cookies. But,
At the end it is printing value something like this:
Hello5BD0268F522455DA719130360F74A969
What I am doing wrong here ???
Server: Apache Tomcat. Jdk: 1.7 Os: lubuntu.
Thanks.
Upvotes: 0
Views: 44
Reputation: 2203
You should be adding below code to your servlet2
to get your desired result.The output which you are getting is JSESSIONID.
Cookie[] ck=request.getCookies();
for(int i=0; i<ck.length; i++) {
if("un".equals(ck[i].getName())) {
pw.write("Hello" +ck[i].getValue());
}
}
Upvotes: 1