Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C:: Create a servlet demonstrating the use of session creation anddestruction. Also check whether the user has visited this page first time or has visited earlier also using sessions.
- INPUT::
- Index.html
- <html>
- <body>
- <form action="session" method="get">
- Enter User Id:<input type="text" name="txtName"><br>
- <input type="reset" value="Reset">
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
- Session.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.";
- public class session extends HttpServlet
- {
- public void doGet(HttpServletRequest req. HttpServletResponse res) throws IOException, ServletException
- {
- PrintWriter out-res.getWriter();
- out.println("<html><body>");
- HttpSession hs req.getSession(true);
- if(hs.isNew())
- {
- String name-req.getParameter("txtName");
- hs.setAttribute("uname", name);
- hs.setAttribute("visit","1");
- out.println("<h5>Welcome First Time</h5>");
- else
- {
- out.println("<h5>Welcome Again</h5>");
- int visit-Integer.parseInt((String)hs.getAttribute("visit"))+1;
- out.println("<h5>You visited "+visit+" times</h5>");
- hs.setAttribute("visit", visit);
- }
- out.println("<h5>Your Seesion Id "+hs.getId()+"</h5>");
- out.println("<h5>You Loged in at "+new java.util.Date(hs.getCreation Time())+"</h5>");
- out.println("<h5><a href=logoutservlet>Click to LOGOUT</a></h5>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- wqlogoutServlet.java
- package logoutservlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
- public class logoutservlet extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
- {
- PrintWriter out=res.getWriter();
- out.println("<html>");
- out.println("<body>");
- HttpSession hs=req.getSession();
- if(hs!=null);
- {
- hs.invalidate();
- }
- out.println("<h1>You are logout now...</h1>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- INPUT::
- Index.html::
- <html><body>
- <a href="download">Download the file</a>
- </body></html>
- Download.java::
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class download extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
- PrintWriter out res.getWriter();
- String filename="sam.txt"; String filepath="D:\\":
- res.setHeader("Content-Disposition","attachment; filename="+filename);
- FileInputStream f=new FileInputStream(filepath+filename);
- int i;
- while((i=f.read())!=-1){
- out.write(i):
- }
- f.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement