index.html
[html]
<form action="servlet1" method="get">
Name:<input type="text" name="userName"/>
<input type="submit" value="go"/>
</form>
[/html]
Here the developer just created a text box to enter the name and also created a submit button.
web.xml
[xml]
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>servletcookies.DemoCookies</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>servletcookies.ShowCookieData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name> should be same and <strong>URL</strong> name should be match with <strong>HTML</strong> form.
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Make sure that
DemoCookies.java
[java]
package servletcookies;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoCookies extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
Cookie ck=new Cookie("uname",n);//creating cookie object
response.addCookie(ck);//adding cookie in the response
//creating submit button
out.print("
<form action='servlet2'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
out.close();
}catch(Exception e){System.out.println(e);}
}
}
[/java]
The
getParameter() returns http request parameters. Those passed from the client to the server. The
doGet() method is utilized to send parameters to an URL along with the header information.
ShowCookieData.java
[java]
package servletcookies;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowCookieData extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
}
}
[/java]
Here created an object to the cookie,
getCookies() Returns an array containing all of the Cookie objects the client sent with this request. This method returns null if no cookies were sent.
Output
The form page get displayed as shown below when complied the code. Where enter the message.
When click on
go button following result will be displayed.
Now click on the
go button following message will be displayed.
Difference Between Cookies And Sessions
Sessions are server-side files that contain user information, while Cookies are client-side files that contain user information. Sessions have a unique identifier that maps them to specific users. This identifier can be passed in the URL or saved into a session cookie. Most of the sites use the second approach, saving the identifier in a Cookie instead of passing it in a URL (which poses a security risk).