Steps: (using notepad)
1. create any folder Example SessionExample
Maintain the folder structure
that is:
1. create SiteHome.html
<html>
<head>
<title>URL Rewriting Example</title>
</head>
<body>
This is the Home page for the example to demonstrate how to use <BR>
URL Rewriting and Hidden form fields to mantain the client state. <BR><BR>
<a href="Login.html">Login</a>
</body>
</html>
2. create
Login.html
<html>
<head>
<title>URL Rewriting Example</title>
</head>
<body>
<form name="loginform" action="login" method="post">
<table border="0">
<tr>
<td>UserName:</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
3. create
Send.html
<html>
<body>
Page Underconstruction
</body>
</html>
4. create folder name is
WEB-INF
Go to
WEB-INF,
(a) create
classes folder
(b) create
lib folder
(c) create
web.xml file
5. Go to classes folder,
write LoginServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname=request.getParameter("uname");
String pass=request.getParameter("password");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if (uname==null||uname.equals("")){
out.println("<b><i>Username Cannot be empty</i></b>");
RequestDispatcher rd=request.getRequestDispatcher("/Login.html");
rd.include(request, response);
return;
}
if (pass==null||pass.equals("")){
out.println("<b><i>Password Cannot be empty</i></b>");
RequestDispatcher rd=request.getRequestDispatcher("/Login.html");
rd.include(request, response);
return;
}
UserDAO ud=new UserDAO();
if(ud.validate(uname,pass)){
out.println("<table width=98% height=95% border=1><tr>");
out.println("<td height=45 colspan=2 align=center><font size=5>My Email Site</font></td>");
out.println("</tr><tr>");
out.println("<td width=12% height=545 align=center valign=top>");
out.println("<p> </p><p><font size=4>");
out.println("<a href='inbox?uname="+uname+"'>InBox</a>");
out.println("</font></p>");
out.println("<p><font size=4>Bulk Mail</font></p>");
out.println("<p><font size=4>Sent Items</font></p>");
out.println("<p><font size=4>Write Mail</font></p>");
out.println("<p><font size=4><a href='Login.html'>Logout</a></font></p>");
out.println("<p> </p></td>");
out.println("<td width=88% align=left valign=top><p> </p>");
out.println("<p><font size=4>Welcome, "+uname+"</font></p></td>");
out.println("</tr><tr align=center>");
out.println("<td colspan=2><div align=center>@Copyrights 2001-08</div></td>");
out.println("</tr></table>");
}//if
else{
out.println("<b><i>Username or Password given are not valid</i></b>");
RequestDispatcher rd=request.getRequestDispatcher("/Login.html");
rd.include(request, response);
return;
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
SETTING THE CLASSPATH FOR TOM CAT SERVER
1. GO TO TOMCAT SERVER INSTALLED FOLDER, LIB FOLDER AND SELECT servlet-api .jar(if you are using servlet program, in case u are using jsp programs, u have to set classpath as jsp-api.jar file)
2. set the class path for mysql-connector.jar file if you are using database connectivity (Ex: my sql database s/w) you have to download "mysql-connector.jar" file and save it in any folder (for example here i have saved that my sql connector.jar file in "D:\softwares\MySQL\new" folder
now we need to set the classpath for servlets and mysql connector jar file
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>
set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.18-bin.jar;.;
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>set CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac LoginServlet.java
Write DriverConnection.java
package com.rajendra.servlets;
import java.sql.*;
public class DriverConnection {
public static Connection getConnection()
throws Exception{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","admin");
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac DriverConnection.java
write UserDao.java
package com.rajendra.servlets;
import java.sql.*;
public class UserDAO {
public boolean validate(String uname, String pass){
try {
Connection con=DriverConnection.getConnection();//getConnection() is a user defined class, which connects to the database
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(
"select count(*) from userdetails where uname=\'"+uname+"\' and pass=\'"+pass+"\'");
return rs.next();
}//try
catch(Exception e){
e.printStackTrace();
}
return false;
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac UserDAO.java
//InboxServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InboxServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uname=request.getParameter("uname");
out.println("<html><head>");
out.println("<title>InBox Page</title>");
out.println("</head><body>");
out.println("<table width=98% height=95% border=1><tr>");
out.println("<td height=45 colspan=2 align=center><font size=5>My Email Site</font></td>");
out.println("</tr><tr>");
out.println("<td width=12% height=545 align=center valign=top>");
out.println("<p> </p><p><font size=4>");
out.println("<a href='inbox?uname="+uname+"'>InBox</a>");
out.println("</font></p>");
out.println("<p><font size=4>Bulk Mail</font></p>");
out.println("<p><font size=4>Sent Items</font></p>");
out.println("<p><font size=4>Write Mail</font></p>");
out.println("<p><font size=4><a href='Login.html'>Logout</a></font></p>");
out.println("<p> </p></td>");
out.println("<td width=88% align=left valign=top><p><font size=4>Welcome, "+uname+"</font></p>");
out.println("<table width=100% height=82 border=0 cellpadding=0 cellspacing=0>");
out.println("<tr bgcolor=#CCCCCC>");
out.println("<th width=8%> </th>");
out.println("<th width=26% align=center>From</th>");
out.println("<th width=41%>Subject</th>");
out.println("<th width=25%>Sent Date</th>");
out.println("</tr>");
MailsDAO md=new MailsDAO();
Collection c=md.getMails(uname);
Iterator i=c.iterator();
while (i.hasNext()){
Mail m=(Mail)i.next();
out.println("<tr>");
out.println("<td align=center><input type=checkbox name=checkbox></td>");
out.println("<td align=left>"+m.getFromid()+"</td>");
out.println("<td><a href='viewMail?id="+m.getMailid()+"&uname="+uname+"'>"+m.getSubject()+"</a></td>");
out.println("<td>"+m.getSentDate()+"</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("<p><font size=4></font></p>");
out.println("</td></tr><tr align=center>");
out.println("<td colspan=2><div align=center>@Copyrights 2001-08</div></td>");
out.println("</tr></table>");
out.println("</body></html>");
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac InboxServlet.java
MailDAO.java
package com.rajendra.servlets;
import java.util.*;
import java.sql.*;
public class MailsDAO {
public Collection getMails(String uname){
/*
* Connect to Mail server and get all the mails of the given username
* This can be done using Java Mail API
* Java Mail API concept
* and thus for this example we will get the mails from DB
* insted of getting diverted towards Java Mail API
*/
try {
Connection con=DriverConnection.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(
"select fromid, subject, sentdate from mails where uname='"+uname+"'");
ArrayList al=new ArrayList();
int count=1;
while (rs.next()){
Mail m=new Mail();
m.setMailid(count+"");
m.setFromid(rs.getString(1));
m.setSubject(rs.getString(2));
m.setSentDate(rs.getString(3));
al.add(m);
count++;
}
return al;
}
catch(Exception e){e.printStackTrace();}
return null;
}
public Mail getMail(String uname, String id){
try {
Connection con=DriverConnection.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(
"select fromid, subject, sentdate, content from mails where uname='"+uname+"' and mid="+id);
if (rs.next()){
Mail m=new Mail();
m.setFromid(rs.getString(1));
m.setSubject(rs.getString(2));
m.setSentDate(rs.getString(3));
m.setContent(rs.getString(4));
return m;
}
}
catch(Exception e){e.printStackTrace();}
return null;
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac MailsDAO .java
Mail.java
package com.rajendra.servlets;
public class Mail {
private String mailid, fromid, subject, sentDate, content;
public String getFromid() {
return fromid;
}
public void setFromid(String fromid) {
this.fromid = fromid;
}
public String getMailid() {
return mailid;
}
public void setMailid(String mailid) {
this.mailid = mailid;
}
public String getSentDate() {
return sentDate;
}
public void setSentDate(String sentDate) {
this.sentDate = sentDate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac Mail.java .java
ViewMailServlet.java
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ViewMailServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uname=request.getParameter("uname");
String mail_no=request.getParameter("id");
MailsDAO md=new MailsDAO();
Mail m=md.getMail(uname, mail_no);
out.println("<html><head>");
out.println("<title>View Mail Page</title>");
out.println("</head><body>");
out.println("<table width=98% height=95% border=1><tr> ");
out.println("<td height=45 colspan=2 align=center><font size=5>My Email Site</font></td>");
out.println("</tr><tr> ");
out.println("<td width=12% height=545 align=center valign=top> ");
out.println("<p> </p><p><font size=4>");
out.println("<a href='inbox?uname="+uname+"'>InBox</a>");
out.println("</font></p>");
out.println("<p><font size=4>Bulk Mail</font></p>");
out.println("<p><font size=4>Sent Items</font></p>");
out.println("<p><font size=4>Write Mail</font></p>");
out.println("<p><font size=4><a href='Login.html'>Logout</a></font></p>");
out.println("<p> </p></td>");
out.println("<td width=88% align=left valign=top><p><font size=4>Welcome, "+uname+"</font></p>");
out.println("<table width=100% height=100% border=0 cellpadding=0 cellspacing=0>");
out.println("<tr bgcolor=#CCCCCC> ");
out.println("<th height=34 colspan=2 bgcolor=#CCCCCC>Read Mail</th>");
out.println("</tr><tr bgcolor=#CCCCCC> ");
out.println("<td width=37% height=54 bgcolor=#FFFFFF>From ID : <strong>"+m.getFromid()+"</strong></td>");
out.println("<td width=63% bgcolor=#FFFFFF>Subject : <strong>"+m.getSubject()+"</strong></td>");
out.println("</tr><tr bgcolor=#FFFFFF> ");
out.println("<td height=36 colspan=2><form method=post action=sendReply>");
out.println("<input type=submit name=Submit value=Reply>");
out.println("<input name=uname type=hidden value='"+uname+"'>");
out.println("<input name=fromid type=hidden value='"+m.getFromid()+"'>");
out.println("<input name=subject type=hidden value='"+m.getSubject()+"'>");
out.println("<input name=content type=hidden value='"+m.getContent()+"'>");
out.println("</form></td></tr>");
out.println("<tr valign=top bgcolor=#CCCCCC> ");
out.println("<td height=375 colspan=2 bgcolor=#FFFFFF><p><strong>Message</strong>:</p>");
out.println("<blockquote> ");
out.println("<p>"+m.getContent()+"</p>");
out.println("</blockquote></td>");
out.println("</tr></table>");
out.println("</td></tr>");
out.println("<tr align=center> ");
out.println("<td colspan=2><div align=center>@Copyrights 2001-08</div></td>");
out.println("</tr></table>");
out.println("</body></html>");
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac ViewMailServlet.java
/*SendReplyServlet.java*/
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendReplyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uname=request.getParameter("uname");
String fromid=request.getParameter("fromid");
String subject=request.getParameter("subject");
String content=request.getParameter("content");
out.println("<html><head>");
out.println("<title>Reply Page</title>");
out.println("</head><body>");
out.println("<table width=98% height=95% border=1><tr>");
out.println("<td height=45 colspan=2 align=center><font size=5>My Email Site</font></td>");
out.println("</tr><tr>");
out.println("<td width=12% height=545 align=center valign=top>");
out.println("<p> </p><p><font size=4>");
out.println("<a href='inbox?uname="+uname+"'>InBox</a>");
out.println("</font></p>");
out.println("<p><font size=4>Bulk Mail</font></p>");
out.println("<p><font size=4>Sent Items</font></p>");
out.println("<p><font size=4>Write Mail</font></p>");
out.println("<p><font size=4><a href='Login.html'>Logout</a></font></p>");
out.println("<p> </p></td>");
out.println("<td width=88% align=left valign=top><p><font size=4>Welcome, "+uname+"</font></p>");
out.println("<form name=form1 method=post action=/Send.html>");
out.println("<table width=100% height=100% border=0 cellpadding=0 cellspacing=0>");
out.println("<tr bgcolor=#CCCCCC> ");
out.println("<th height=34 colspan=2 bgcolor=#CCCCCC>Read Mail</th>");
out.println("</tr>");
out.println("<tr bgcolor=#CCCCCC> ");
out.println("<td width=15% height=38 bgcolor=#FFFFFF>To : </td>");
out.println("<td width=85% bgcolor=#FFFFFF><input name=to type=text id=to value="+fromid+"></td>");
out.println("</tr>");
out.println("<tr bgcolor=#CCCCCC> ");
out.println("<td height=35 bgcolor=#FFFFFF>Subject : </td>");
out.println("<td bgcolor=#FFFFFF><strong>");
out.println("<input name=subject type=text id=subject value='Re: "+subject+"'>");
out.println("</strong></td></tr>");
out.println("<tr bgcolor=#CCCCCC> ");
out.println("<td height=35 bgcolor=#FFFFFF><strong>Message:</strong></td>");
out.println("<td bgcolor=#FFFFFF> </td>");
out.println("</tr>");
out.println("<tr align=left valign=top bgcolor=#CCCCCC> ");
out.println("<td colspan=2 bgcolor=#FFFFFF><textarea name=content cols=100 rows=20 id=content>"+content+"</textarea></td>");
out.println("</tr>");
out.println("<tr bgcolor=#CCCCCC> ");
out.println("<td height=35 bgcolor=#FFFFFF> </td>");
out.println("<td bgcolor=#FFFFFF><input type=submit value=Send></td>");
out.println("</tr></table>");
out.println("</form></td></tr>");
out.println("<tr align=center> ");
out.println("<td colspan=2><div align=center>@Copyrights 2001-08</div></td>");
out.println("</tr></table>");
out.println("</body></html>");
}
}
D:\webtechnologies\servlets\session tracking\sessionex1\WEB-INF\classes>javac SendReplyServlet.java
- Now go to WEB-INF folder, open lib folder paste the mysql-connector-java-5.1.18-bin.jar file in that folder
(c) create web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>InboxServlet</servlet-name>
<servlet-class>com.rajendra.servlets.InboxServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ViewMailServlet</servlet-name>
<servlet-class> com.rajendra.servlets.ViewMailServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SendReplyServlet</servlet-name>
<servlet-class> com.rajendra.servlets.SendReplyServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>InboxServlet</servlet-name>
<url-pattern>/inbox</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewMailServlet</servlet-name>
<url-pattern>/viewMail</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SendReplyServlet</servlet-name>
<url-pattern>/sendReply</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>SiteHome.html</welcome-file>
</welcome-file-list>
</web-app>
- now deploy our project (i.e SessionExample) into Server.
- go to installed tomcat server
- C:\Program Files\Apache Software Foundation\Tomcat 6.0
- double click on webapps folder
- deploy (paste) our project(i.e SessionExample)
- start the server: go to bin folder of Tomcat 6.0->double click on tomcat6.exe file
- once the server start
- go to any web browser
- in the address bar type : step 1 http://localhost:8888
- here 8888 is my system port number
- step 2: click on Tomcat manager:
http://localhost:8888/SessionExample/