Advertisement
zoro-10

servlet

Jul 28th, 2024 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. 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.
  2. INPUT::
  3.  
  4. Index.html
  5. <html>
  6. <body>
  7. <form action="session" method="get">
  8. Enter User Id:<input type="text" name="txtName"><br>
  9. <input type="reset" value="Reset">
  10. <input type="submit" value="Submit">
  11. </form>
  12. </body>
  13. </html>
  14.  
  15. Session.java
  16. import java.io.*;
  17. import javax.servlet.*;
  18. import javax.servlet.http.";
  19. public class session extends HttpServlet
  20. {
  21.  public void doGet(HttpServletRequest req. HttpServletResponse res) throws IOException, ServletException
  22.  {
  23.    PrintWriter out-res.getWriter();
  24.    out.println("<html><body>");
  25.    HttpSession hs req.getSession(true);
  26.    if(hs.isNew())
  27.    {
  28.      String name-req.getParameter("txtName");
  29.      hs.setAttribute("uname", name);
  30.      hs.setAttribute("visit","1");
  31.      out.println("<h5>Welcome First Time</h5>");
  32.    else
  33.    {
  34.      out.println("<h5>Welcome Again</h5>");
  35.      int visit-Integer.parseInt((String)hs.getAttribute("visit"))+1;
  36.      out.println("<h5>You visited "+visit+" times</h5>");
  37.      hs.setAttribute("visit", visit);
  38.    }
  39.    out.println("<h5>Your Seesion Id "+hs.getId()+"</h5>");
  40.    out.println("<h5>You Loged in at "+new java.util.Date(hs.getCreation Time())+"</h5>");
  41.    out.println("<h5><a href=logoutservlet>Click to LOGOUT</a></h5>");
  42.    out.println("</body>");
  43.    out.println("</html>");
  44.  }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51. wqlogoutServlet.java
  52. package logoutservlet;
  53. import java.io.IOException;
  54. import java.io.PrintWriter;
  55. import javax.servlet.ServletException;
  56. import javax.servlet.http.*;
  57. public class logoutservlet extends HttpServlet {
  58.  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,   ServletException
  59.  {
  60.    PrintWriter out=res.getWriter();
  61.    out.println("<html>");
  62.    out.println("<body>");
  63.    HttpSession hs=req.getSession();
  64.    if(hs!=null);
  65.    {
  66.      hs.invalidate();
  67.    }
  68.    out.println("<h1>You are logout now...</h1>");
  69.    out.println("</body>");
  70.    out.println("</html>");
  71.  }
  72. }
  73.  
  74.  
  75.  
  76.  
  77. INPUT::
  78.  
  79. Index.html::
  80. <html><body>
  81. <a href="download">Download the file</a>
  82. </body></html>
  83.  
  84. Download.java::
  85. import java.io.*;
  86. import javax.servlet.*;
  87. import javax.servlet.http.*;
  88. public class download extends HttpServlet {
  89.  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,   ServletException{
  90.    PrintWriter out res.getWriter();
  91.    String filename="sam.txt"; String filepath="D:\\":
  92.    res.setHeader("Content-Disposition","attachment; filename="+filename);
  93.    FileInputStream f=new FileInputStream(filepath+filename);
  94.    int i;
  95.    while((i=f.read())!=-1){
  96.      out.write(i):
  97.    }
  98.    f.close();
  99.  }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement