Advertisement
andrzejiwaniuk

web servlet - J2EE

Jan 18th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.11 KB | None | 0 0
  1. /*
  2.      Web Servlet - J2EE
  3.  
  4. */
  5.  
  6. package tedoweb1;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.Cookie;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18. //
  19. /**
  20.  * Servlet implementation class Fajna
  21.  */
  22. /*@WebServlet("/Hello1.do")*/
  23. public class Fajna extends HttpServlet {
  24.     private static final long serialVersionUID = 1L;
  25.    
  26.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  27.     throws ServletException, IOException
  28.     {
  29.         //ciekawe(request, response);
  30.         ciekawe2(request, response);
  31.         //ciekawe3(request, response);
  32.     }
  33.    
  34.     /**
  35.      * @see HttpServlet#HttpServlet()
  36.      */
  37.     public Fajna() {
  38.         super();
  39.         // TODO Auto-generated constructor stub
  40.     }
  41.  
  42.     /**
  43.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  44.      */
  45.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  46.         // TODO Auto-generated method stub
  47.         //response.getWriter().append("Served at: ").append(request.getContextPath());
  48.         processRequest(request, response);
  49.     }
  50.  
  51.     /**
  52.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  53.      */
  54.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  55.         // TODO Auto-generated method stub
  56.         //doGet(request, response);
  57.         processRequest(request, response);
  58.     }
  59.    
  60.     //przyklad uzycia forward do pliku pokaz.jsp
  61.         void ciekawe3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
  62.             response.setContentType("text/html;charset=UTF-8");
  63.             PrintWriter out = response.getWriter();
  64.             /*HttpSession ssn = request.getSession();
  65.            
  66.            
  67.             if(ssn != null){
  68.                 out.println("Your session Id is : ");
  69.                 String ssnId = ssn.getId();
  70.                 out.println(ssnId);
  71.                
  72.             }*/
  73.  
  74.             try{
  75.                 request.setAttribute("MD5", dane());
  76.                 request.getRequestDispatcher("/pokaz.jsp").forward(request, response);
  77.             }finally{
  78.                 out.close();
  79.             }
  80.         }
  81.     private String dane(){
  82.         return "10984938";
  83.     }
  84.    
  85.     //przyklad uzycia sesji
  86.     void ciekawe2(HttpServletRequest request, HttpServletResponse response) throws IOException{
  87.         response.setContentType("text/html;charset=UTF-8");
  88.         PrintWriter out = response.getWriter();
  89.         HttpSession ssn = request.getSession();
  90.        
  91.         Integer visitCount = new Integer(0);
  92.         String visitCountKey = new String("visitCount");
  93.         String userIDKey = new String("userID");
  94.         String userID = new String("ABCD");
  95.        
  96.         // Check if this is new comer on your web page.
  97.          /* if (ssn.isNew()){
  98.              out.println("Witaj po raz pierwszy na stronie");
  99.               //ssn.setAttribute(userIDKey, userID);
  100.           } else {
  101.              visitCount = (Integer)ssn.getAttribute(visitCountKey);
  102.              visitCount = visitCount + 1;
  103.              out.println("licznik wizyt: " + visitCount);
  104.            }
  105.           ssn.setAttribute(visitCountKey,  visitCount);*/
  106.         if(ssn != null){
  107.             out.println("Your session Id is : ");
  108.             String ssnId = ssn.getId();
  109.             out.println(ssnId);
  110.            
  111.         }
  112.  
  113.         try{
  114.             /*if (ssn.isNew()){
  115.              out.println("Witaj po raz pierwszy na stronie");
  116.               ssn.setAttribute(userIDKey, userID);
  117.           } else {
  118.              visitCount = (Integer)ssn.getAttribute(visitCountKey);
  119.              visitCount = visitCount + 1;
  120.              userID = (String)ssn.getAttribute(userIDKey);
  121.              
  122.            }
  123.           ssn.setAttribute(visitCountKey,  visitCount);
  124.           out.println("licznik wizyt: " + visitCount);*/
  125.          
  126.             /*if (request.getSession()==null)
  127.             {
  128.                 out.println("Witaj na stronie pierwszy raz");
  129.                 request.getSession();
  130.             }else
  131.                 out.println("Witaj na stronie kolejny raz");*/
  132.            
  133.             out.println("Autorem skryptu jest " + this.getInitParameter("Autor"));
  134.         }finally{
  135.             out.close();
  136.         }
  137.     }
  138.    
  139.    
  140.     void ciekawe(HttpServletRequest request, HttpServletResponse response) throws IOException{
  141.         response.setContentType("text/html;charset=UTF-8");
  142.         PrintWriter out = response.getWriter();
  143.         try{
  144.             String login = request.getParameter("imie");
  145.             //wyslij kod bledu
  146.             //response.sendError(404, "numer telefonu");
  147.             //redirect to another web site
  148.             //response.sendRedirect("http://www.tedo-tech.co.uk");
  149.             out.println("<html>");
  150.             out.println("<head>");
  151.             out.println("<title>Servlet Wita </title>");
  152.             out.println("</head>");
  153.             out.println("<body");
  154.            
  155.            
  156.             //out.println(request.getContextPath());
  157.             out.println(request.getServletPath());
  158.             //out.println(request.getQueryString());
  159.             /*Cookie[] cookies1 = request.getCookies();
  160.             if(cookies1!=null)
  161.             for (int i = 0; i < cookies1.length; i++) {
  162.              cookies1[i].setMaxAge(0);
  163.             }*/
  164.             //usuwanie Cookie
  165.             /*Cookie cookie = new Cookie("color", "");
  166.  
  167.             cookie.setMaxAge(0);
  168.  
  169.             response.addCookie(cookie);*/
  170.            
  171.             boolean foundCookie = false;
  172.             Cookie[] cookies = request.getCookies();
  173.            
  174.  
  175.             for(int i = 0; i < cookies.length; i++) {
  176.                 Cookie cookie1 = cookies[i];
  177.                 if (cookie1.getName().equals("color")) {
  178.                     out.println("bgcolor = " + cookie1.getValue());
  179.                     foundCookie = true;
  180.                 }
  181.             }  
  182.  
  183.             if (!foundCookie) {
  184.                 Cookie cookie1 = new Cookie("color", "yellow");
  185.                 cookie1.setMaxAge(24*60*60);
  186.                 response.addCookie(cookie1);
  187.             }
  188.            
  189.             out.println(">");
  190.             out.println("<H1>Setting and Reading Cookies</H1>");
  191.             out.println("This page will set its background color using a cookie when reloaded.");
  192.             out.println("<h1>Witaj " + login + ", na tym swiecie!</h1>");
  193.             out.println("</BODY>");
  194.             out.println("</HTML>");
  195.            
  196.         }finally{
  197.             out.close();
  198.         }
  199.        
  200.     }
  201.  
  202. }
  203. /*
  204. *   Maven POM
  205. *
  206. */
  207. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  208.   <modelVersion>4.0.0</modelVersion>
  209.   <groupId>tedoweb1</groupId>
  210.   <artifactId>tedoweb1</artifactId>
  211.   <version>0.0.1-SNAPSHOT</version>
  212.   <packaging>war</packaging>
  213.   <build>
  214.     <sourceDirectory>src</sourceDirectory>
  215.     <plugins>
  216.       <plugin>
  217.         <artifactId>maven-war-plugin</artifactId>
  218.         <version>2.6</version>
  219.         <configuration>
  220.           <warSourceDirectory>WebContent</warSourceDirectory>
  221.           <failOnMissingWebXml>false</failOnMissingWebXml>
  222.         </configuration>
  223.       </plugin>
  224.       <plugin>
  225.         <artifactId>maven-compiler-plugin</artifactId>
  226.         <version>3.3</version>
  227.         <configuration>
  228.           <source>1.8</source>
  229.           <target>1.8</target>
  230.         </configuration>
  231.       </plugin>
  232.     </plugins>
  233.   </build>
  234.  
  235.   <dependencies>
  236.         <dependency>
  237.             <groupId>asm</groupId>
  238.             <artifactId>asm-all</artifactId>
  239.             <version>3.3.1</version>
  240.         </dependency>
  241.         <dependency>
  242.             <groupId>com.sun.jersey</groupId>
  243.             <artifactId>jersey-bundle</artifactId>
  244.             <version>1.14</version>
  245.         </dependency>
  246.         <dependency>
  247.             <groupId>org.json</groupId>
  248.             <artifactId>json</artifactId>
  249.             <version>20090211</version>
  250.         </dependency>
  251.        
  252.         <dependency>
  253.             <groupId>javax.servlet</groupId>
  254.             <artifactId>servlet-api</artifactId>
  255.             <version>2.5</version>
  256.         </dependency>
  257.        
  258.     </dependencies>
  259. </project>
  260.  
  261. /*
  262.  some jsp file  
  263. */
  264.  
  265. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  266.     pageEncoding="ISO-8859-1"%>
  267. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  268. <html>
  269. <head>
  270. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  271. <title>Insert title here</title>
  272. </head>
  273. <body>
  274. <form action="Hello1.do">
  275.     <p> wprowadz imie:</p>
  276.     <input type="text" name="imie" />
  277.     <input type="submit" name="OK" />
  278. </form>
  279. </body>
  280. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement