JSP
JSP Current Date and Time
JSP Date Handling All methods of core Java can be used in JSP is the biggest advantage of JSP. In...
Following are the cookies methods:
It is used to set the domain to which the cookie applies
It is used to get the domain to which cookie applies
It sets the maximum time which should apply till the cookie expires
It returns the maximum age of cookie
It returns the name of the cookie
Sets the value associated with the cookie
Get the value associated with the cookie
It sets the path to which cookie applies
It gets the path to which the cookie applies
It should be sent over encrypted connections or not.
It describes the cookie purpose
It the returns the cookie comments which has been described.
In this example, we are creating cookies of username and email and add age to the cookie for 10 hours and trying to get the variable names in the action_cookie.jsp
Action_cookie.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Cookie</title> </head> <body> <form action="action_cookie_main.jsp" method="GET"> Username: <input type="text" name="username"> <br /> Email: <input type="text" name="email" /> <input type="submit" value="Submit" /> </form> </body> </html>
Action_cookie_main.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% Cookie username = new Cookie("username", request.getParameter("username")); Cookie email = new Cookie("email", request.getParameter("email")); username.setMaxAge(60*60*10); email.setMaxAge(60*60*10); // Add both the cookies in the response header. response.addCookie( username ); response.addCookie( email ); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Cookie JSP</title> </head> <body> <b>Username:</b> <%= request.getParameter("username")%> <b>Email:</b> <%= request.getParameter("email")%> </body> </html>
Explanation of the code:
Action_cookie.jsp
Code Line 10-15: Here we are taking a form which has to be processed in action_cookie_main.jsp. Also, we are taking two fields "username" and "email" which has to be taken input from the user with a submit button.
Action_cookie_main.jsp
Code Line 6-9: Creating two cookie objects of "username" and "email" using request.getParameter.
Code Line 12-13: Here we are adding age to both the cookies, which have been created of 10 hours i.e. cookies will expire in that age.
Code Line 16-17: Adding cookies to the session of username and email and these two cookies can fetched when requested by getParameter().
Output:
When you execute the above code you get the following output:
When we execute the action_cookie.jsp we get two fields username and email, and it takes user input and then we click on the submit button.
We get the output from action_cookie_main.jsp where variables are stored in the cookies on the client side.
JSP Date Handling All methods of core Java can be used in JSP is the biggest advantage of JSP. In...
What is JSP Exception? Exceptions occur when there is an error in the code either by the developer...
What is JSP Implicit object? JSP implicit objects are created during the translation phase of JSP...
Download PDF Following Spring interview questions are for freshers and experienced users 1) What is...
What is JSP LifeCycle? JSP Life Cycle is defined as translation of JSP Page into servlet as a JSP...
In this example, we are going to learn about uploading and downloading of a file through JSP. File...