JSP Tutorial for Beginner
When
we submit this form to web server, first it will check and validate.
onSubmit="return validateForm()" event check validateForm() function
in javascript, and if any field find blank it will throw error alert on
browser. This submit only when validateForm() function return true. This
results in jump to next form validateInput.jsp of JSP page.
What is cookie
Jsp Tutorial for Beginner
What is jsp ?
Jsp is easiest and powerful scripting language because it is
compiled by web container/servlet container.
Jsp is used to create dynamic web pages for the interaction
of end user with the help of jsp form or html form.
Jsp is provided by sun Microsystems. Jsp is first compiled
and then make servlet. Jsp takes request from
Browser and process this request by web server and response
send back to browser.
JSP code can be put inside HTML code. Both code work together, JSP code be embedded
with scriptlet <% inside this jsp code %>
JSP setup and Installation
Jsp need any web server like weblogic, Websphere,
tomcat. All jsp should be deployed into
web server
We will use Tomcat server to run JSP, this Tomcat server can
run on any platform like windows or linux.
First Install tomcat and deploy jsp in tomcat container as
the following directory structure
Tomcat
Bin
Conf
Lib
Logs
Tmp
+Webapps
Doc
Example
File
Host-manager
ROOT
jsp
+Work
Catalina
Bin
Conf
Lib
Logs
Tmp
+Webapps
Doc
Example
File
Host-manager
ROOT
jsp
+Work
Catalina
We will use our own application context,
so have to make our own folder in webapps folder with jsp name. All jsp file
should be copy in this jsp folder.
JSP forms and User Input
JSP form is a way to interact with user input with web server or
database server. In this, HTML form tag (e.g <input type=”text”
name=”name”>, <input type=”password” name=”name”>, <input
type=”radio” name=”name”>, <input type=”checkbox” name=”name”>) is
used and values inside this form tag can be retrieved by request objects of JSP
engine.
JSP Example of input text
form
Html.jsp :
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="textInput.jsp">
<table width="100%" border="0"
cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>Name
Student </td>
<td><input type="text" name="studentName"></td>
</tr>
<tr>
<td>Active <input type="radio" name="active" value="Active"></td>
<td>DeActive <input type="radio" name="active" value="DeActive"></td>
</tr>
<tr>
<td>State <input type="checkbox" name="state" value="state"></td>
<td>City <input type="checkbox" name="city" value="city"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
Get these form value in JSP
textInput.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String studentNameInJSP=request.getParameter("studentName");
String radioInJSP=request.getParameter("active");
String checkboxStateInJSP=request.getParameter("state");
String checkboxCityInJSP=request.getParameter("city");
%>
<html>
<body>
Value of input text box in JSP : <%=studentNameInJSP%>
Value of checkbox in JSP : <%=checkboxStateInJSP%> <%=checkboxCityInJSP%>
Value of radio button box in JSP : <%=radioInJSP%>
</body>
</html>
Form Validation using javascript in jsp
formValidation.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<head>
<script>
function validateForm()
{
if(document.frm.username.value=="")
{
alert("User Name should be left blank");
document.frm.username.focus();
return false;
}
else if(document.frm.pwd.value=="")
{
alert("Password should be left blank");
document.frm.pwd.focus();
return false;
}
}
</script>
</head>
<body>
<form name="frm" method="get" action="validateInput.jsp" onSubmit="return validateForm()">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>UserName </td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="pwd" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
What is cookie
Cookie is a small data file reside in user’s system. Cookie is
made by web server to identify users. When user do any request send to web
server and web server know information of user by these cookie. After process
request, web server response back to request by knowing through this cookie.
JSP provides cookie classes, javax.servlet.http.Cookie, by
this classes we can create and retrieve cookie. Once we set value in cookie, it
lived until cookie gets expired. Cookie plays a big role in session, because
maximum session is tracked by cookie in JSP.
1. JSP Example of creating Cookie
createCookie.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %> <html> <body> <form name="frm" method="get" action="createNewCookie.jsp"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="22%"> </td> <td width="78%"> </td> </tr> <tr> <td>Cookie value Set </td> <td><input type="text" name="cookieSet" /></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Submit"></td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form> </body> </html>
createNewCookie.jsp
<%@ page language="java" import="java.util.*"%> <% String cookieSet=request.getParameter("cookieSet");
Cookie cookie = new Cookie ("cookieSet",cookieSet); response.addCookie(cookie); %>
<html> <head> <title>Cookie Create Example</title> </head> <body> <% Cookie[] cookies = request.getCookies(); for (int i=0; i<cookies.length; i++) { out.println(cookies[i].getName()+" : "+cookies[i].getValue()+"<br/>"); } %> </body> </html>
Cookie cookie = new Cookie
("cookieSet",cookieSet);
We are creating new object of cookie class.Here new Cookie(“Key”, “value”); Then we are adding in cookie of response object. response.addCookie(cookie); This is adding in cookie object new data.
Expire JSP cookie
Cookie cookie = new Cookie
("cookieSet",cookieSet);
cookie setMaxAge(0);
This will expire cookie.,with 0 second. We can set cookie age who long is reside for user.
Retrieve cookie from JSP
Cookie is in array object, we need to get in
request.getCookies() of array.
Cookie[] cookies = request.getCookies();
for (int i=0; i<cookies.length; i++) {
out.println(cookies[i].getName()+" : "+cookies[i].getValue()+"<br/>"); }
JSP session Object
Session object is medium to interact with client and server. Session is a connection between user and server, involving exchange of information between user’s computer and server. Server knows information about each other by these session object. Web server put information of user in session object and whenever it needs information gets it from these session objects. This session object stores information in key value pair, just like hashtable. Today programming without session cannot thinkable. Most of web application is user based, somewhat use transaction (credit card, database transaction), shopping cart, email, and this needs session. Web server should know who is doing this transaction. This session object helps to differentiate users with each other, and increase application’s security. Every user have unique session, server exchange information with session objects until it get expired or destroyed by web server.
When JSP Session use
Mostly session is work with user base application, when login
screen is used then set session if login is successful. It set for maximum time
provided by web server or defined by application.
When JSP Session destroys
When user has done their work and want to close browser, it should be expire or destroy forcefully by user, ensure no other person can use his session.
JSP Store and Retrieve Session Variables
JSP Example of Creating session and retrieving session
<%@ page contentType="text/html;
charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="sessionSetRetrieve.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>Session value Set </td>
<td><input type="text" name="sessionVariable" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*"%>
<%
String sessionSet=request.getParameter("sessionVariable");
session.setAttribute("MySession",sessionSet);
/// String
getSessionValue= (String)session.getAttribute("sessionSet");
//this is use for
session value in String data
%>
<html>
<head>
<title>Cookie Create Example</title>
</head>
<body>
Session : <%=(String)session.getAttribute("MySession")%>
</body>
</html>
session.setAttribute("MySession",sessionSet) this is
use to set new session variable. If we need to retrieve session variable, have
to use session.getAttribute. In this we have to get it by session variable name
here we are using MySession is
session variable as key.
session.setMaxInactiveInterval(2700);
session.setMaxInactiveInterval(2700), this use to set maximum
session time. 2700 is time in number. In this period, if user don’t do anything
session get expired automatically.
Remove JSP Session
Variables or Expire JSP Session
When session is no long needed, should be removed forcefully by
user. This can be done by calling session method of invalidate method session.invalidate();
session.invalidate();
This method expires session for current user, who request for
log out.
New session can be find by isNew() method of session, when first
time session is created, that is new session.
session.isNew();
|
No comments:
Post a Comment