0% found this document useful (0 votes)
97 views5 pages

Locking Servlet & Session Tracking Hit Count

The lock.java servlet uses init parameters to check the hostname, port, and key to determine if the user is allowed access. It displays a welcome message or access denied. The hits.java servlet tracks user sessions by storing and incrementing a visit count in the session. It displays session information including creation time, last access time, user ID, and visit count. The web.xml files map the servlets to URLs for access control and session tracking.

Uploaded by

Thiyaga Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views5 pages

Locking Servlet & Session Tracking Hit Count

The lock.java servlet uses init parameters to check the hostname, port, and key to determine if the user is allowed access. It displays a welcome message or access denied. The hits.java servlet tracks user sessions by storing and incrementing a visit count in the session. It displays session information including creation time, last access time, user ID, and visit count. The web.xml files map the servlets to URLs for access control and session tracking.

Uploaded by

Thiyaga Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Locking Servlet

lock.java
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class lock extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)throws
ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String key = getInitParameter("key");
String usr=getInitParameter("user");
String host = req.getServerName();
int port = req.getServerPort();
int initport =Integer.parseInt(getInitParameter("port"));
out.println("<br> host =" + host + "<br> key =" + key);
out.println("<br> port =" + port +"<br> initport =" + initport);
if((host.compareTo(key)==0) && port==initport)
out.println("<br> Welcome "+usr);
else
out.println("<br> Sorry! You are not allowed to Access");
}
}

web.xml
<servlet>
<servlet-name>Lock</servlet-name>
<servlet-class>lockingservlet.lock</servlet-class>

<init-param>
<param-name>key</param-name>
<param-value>localhost</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>panimalar</param-value>
</init-param>
<init-param>
<param-name>port</param-name>
<param-value>8080</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Lock</servlet-name>
<url-pattern>/lock</url-pattern>
</servlet-mapping>

Tracking Session using Hit Count


hits.java
package hitcount;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class hits extends HttpServlet {
public void doGet(HttpServletRequest request,
response)

HttpServletResponse

throws ServletException, IOException


{
HttpSession session = request.getSession(true);
Date createTime = new Date(session.getCreationTime());
Date lastAccessTime =
new Date(session.getLastAccessedTime());
String title = "Session tracking. Hit count Example";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("ABCD");
if (session.isNew()){
//title = "Welcome to my website";
session.setAttribute(userIDKey, userID);

} else {
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)session.getAttribute(userIDKey);
}
session.setAttribute(visitCountKey, visitCount);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">Session Infomation</h2>\n" +
"<table border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#949494\">\n" +
" <th>Session info</th><th>value</th></tr>\n" +
"<tr>\n" +
" <td>id</td>\n" +
" <td>" + session.getId() + "</td></tr>\n" +
"<tr>\n" +
" <td>Creation Time</td>\n" +
" <td>" + createTime +
" </td></tr>\n" +
"<tr>\n" +

" <td>Time of Last Access</td>\n" +


" <td>" + lastAccessTime +
" </td></tr>\n" +
"<tr>\n" +
" <td>User ID</td>\n" +
" <td>" + userID +
" </td></tr>\n" +
"<tr>\n" +
" <td>Number of visits</td>\n" +
" <td>" + visitCount + "</td></tr>\n" +
"</table>\n" +
"</body></html>");
}
}

web.xml
<servlet>
<servlet-name>tracks</servlet-name>
<servlet-class>hitcount.hits</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tracks</servlet-name>
<url-pattern>/count</url-pattern>
</servlet-mapping>

You might also like