0% found this document useful (0 votes)
21 views8 pages

Week 678

hello

Uploaded by

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

Week 678

hello

Uploaded by

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

Week6: Design a controller with servlet that provides the interaction with web application

Source Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
Web.xml

<web-app>
<servlet>
<servlet-name>week6</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>week6</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
Week 7: Maintaining the transactional history of any user is very important. Explore the various
session tracking mechanism using Cookies

i) Cookies – Programs

CookiesDemo.html
<html>
<body>
<form action = "CookiesDemo" method = "GET">
First Name: <input type = "text" name = "first_name">
<br><br>
Last Name: <input type = "text" name = "last_name" />
<br><br>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>

CookiesDemo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class CookiesDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

// Create cookies for first and last names.


Cookie firstName = new Cookie("first_name", request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name", request.getParameter("last_name"));

// Set expiry date for both the cookies.


firstName.setMaxAge(60); // 1min-60secs
lastName.setMaxAge(60*60*2); // 2hrs

// Add both the cookies in the response header.


response.addCookie( firstName );
response.addCookie( lastName );

// Set response content type


response.setContentType("text/html");

PrintWriter out = response.getWriter();


out.println("<b> Cookies are created and the cookies are: <b><br>");
out.println("<b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <b>Last Name</b>: "
+ request.getParameter("last_name") + "\n"
);
}
}

ReadCookiesDemo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class ReadCookiesDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//Cookie cookie = null;
Cookie[] cookies = null;

// Get an array of Cookies associated with this domain


cookies = request.getCookies();

// Set response content type


response.setContentType("text/html");

PrintWriter out = response.getWriter();

if( cookies != null ) {


out.println("<h2> Found Cookies Name and Value</h2>");

for (int i = 0; i < cookies.length; i++) {


// cookie = cookies[i];
out.print("Name : " + cookies[i].getName( ) + ", ");
out.print("Value: " + cookies[i].getValue( ) + " <br/>");
}
} else {
out.println("<h2>No cookies founds</h2>");
}

}
}
Web.xml
<web-app>
<servlet>
<servlet-name>cookies</servlet-name>
<servlet-class>CookiesDemo</servlet-class>
</servlet>
<servlet>
<servlet-name>cookies1</servlet-name>
<servlet-class>ReadCookiesDemo</servlet-class>
</servlet>

<servlet-mapping>

<servlet-name>cookies</servlet-name>
<url-pattern>/CookiesDemo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cookies1</servlet-name>
<url-pattern>/ReadCookiesDemo</url-pattern>
</servlet-mapping>
</web-app>

Output:
Week 8: Maintaining the transactional history of any user is very important. Explore the various
session tracking mechanism using Sessions.

HttpSession – Programs

SessionTrack.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// Extend HttpServlet class


public class SessionTrack extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

// Create a session object if it is already not created.


HttpSession s1 = request.getSession(true);

// Get session creation time.


Date createTime = new Date(s1.getCreationTime());

// Get last access time of this web page.


Date lastAccessTime = new Date(s1.getLastAccessedTime());
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("ABCD");

// Check if this is new comer on your web page.


if (s1.isNew())
{
s1.setAttribute(userIDKey, userID);
}
else
{
visitCount = (Integer)s1.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)s1.getAttribute(userIDKey);
}
s1.setAttribute(visitCountKey, visitCount);
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<h1>session id is" + s1.getId() +


"<br>Creation Time"+ createTime +
"<br>lass access time" + lastAccessTime +
"<br>User ID is" +userID +
"<br>Number of visits is" + visitCount+"</h1>" );
}
}

Web.xml
<web-app>
<servlet>
<servlet-name>Session1</servlet-name>
<servlet-class>SessionTrack</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session1</servlet-name>
<url-pattern>/SessionTrack</url-pattern>
</servlet-mapping>
</web-app>

Output:

You might also like