Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Web Servlet - J2EE
- */
- package tedoweb1;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- //
- /**
- * Servlet implementation class Fajna
- */
- /*@WebServlet("/Hello1.do")*/
- public class Fajna extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- //ciekawe(request, response);
- ciekawe2(request, response);
- //ciekawe3(request, response);
- }
- /**
- * @see HttpServlet#HttpServlet()
- */
- public Fajna() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- //response.getWriter().append("Served at: ").append(request.getContextPath());
- processRequest(request, response);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- //doGet(request, response);
- processRequest(request, response);
- }
- //przyklad uzycia forward do pliku pokaz.jsp
- void ciekawe3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- /*HttpSession ssn = request.getSession();
- if(ssn != null){
- out.println("Your session Id is : ");
- String ssnId = ssn.getId();
- out.println(ssnId);
- }*/
- try{
- request.setAttribute("MD5", dane());
- request.getRequestDispatcher("/pokaz.jsp").forward(request, response);
- }finally{
- out.close();
- }
- }
- private String dane(){
- return "10984938";
- }
- //przyklad uzycia sesji
- void ciekawe2(HttpServletRequest request, HttpServletResponse response) throws IOException{
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- HttpSession ssn = request.getSession();
- 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 (ssn.isNew()){
- out.println("Witaj po raz pierwszy na stronie");
- //ssn.setAttribute(userIDKey, userID);
- } else {
- visitCount = (Integer)ssn.getAttribute(visitCountKey);
- visitCount = visitCount + 1;
- out.println("licznik wizyt: " + visitCount);
- }
- ssn.setAttribute(visitCountKey, visitCount);*/
- if(ssn != null){
- out.println("Your session Id is : ");
- String ssnId = ssn.getId();
- out.println(ssnId);
- }
- try{
- /*if (ssn.isNew()){
- out.println("Witaj po raz pierwszy na stronie");
- ssn.setAttribute(userIDKey, userID);
- } else {
- visitCount = (Integer)ssn.getAttribute(visitCountKey);
- visitCount = visitCount + 1;
- userID = (String)ssn.getAttribute(userIDKey);
- }
- ssn.setAttribute(visitCountKey, visitCount);
- out.println("licznik wizyt: " + visitCount);*/
- /*if (request.getSession()==null)
- {
- out.println("Witaj na stronie pierwszy raz");
- request.getSession();
- }else
- out.println("Witaj na stronie kolejny raz");*/
- out.println("Autorem skryptu jest " + this.getInitParameter("Autor"));
- }finally{
- out.close();
- }
- }
- void ciekawe(HttpServletRequest request, HttpServletResponse response) throws IOException{
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try{
- String login = request.getParameter("imie");
- //wyslij kod bledu
- //response.sendError(404, "numer telefonu");
- //redirect to another web site
- //response.sendRedirect("http://www.tedo-tech.co.uk");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet Wita </title>");
- out.println("</head>");
- out.println("<body");
- //out.println(request.getContextPath());
- out.println(request.getServletPath());
- //out.println(request.getQueryString());
- /*Cookie[] cookies1 = request.getCookies();
- if(cookies1!=null)
- for (int i = 0; i < cookies1.length; i++) {
- cookies1[i].setMaxAge(0);
- }*/
- //usuwanie Cookie
- /*Cookie cookie = new Cookie("color", "");
- cookie.setMaxAge(0);
- response.addCookie(cookie);*/
- boolean foundCookie = false;
- Cookie[] cookies = request.getCookies();
- for(int i = 0; i < cookies.length; i++) {
- Cookie cookie1 = cookies[i];
- if (cookie1.getName().equals("color")) {
- out.println("bgcolor = " + cookie1.getValue());
- foundCookie = true;
- }
- }
- if (!foundCookie) {
- Cookie cookie1 = new Cookie("color", "yellow");
- cookie1.setMaxAge(24*60*60);
- response.addCookie(cookie1);
- }
- out.println(">");
- out.println("<H1>Setting and Reading Cookies</H1>");
- out.println("This page will set its background color using a cookie when reloaded.");
- out.println("<h1>Witaj " + login + ", na tym swiecie!</h1>");
- out.println("</BODY>");
- out.println("</HTML>");
- }finally{
- out.close();
- }
- }
- }
- /*
- * Maven POM
- *
- */
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>tedoweb1</groupId>
- <artifactId>tedoweb1</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <build>
- <sourceDirectory>src</sourceDirectory>
- <plugins>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <warSourceDirectory>WebContent</warSourceDirectory>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.3</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>asm</groupId>
- <artifactId>asm-all</artifactId>
- <version>3.3.1</version>
- </dependency>
- <dependency>
- <groupId>com.sun.jersey</groupId>
- <artifactId>jersey-bundle</artifactId>
- <version>1.14</version>
- </dependency>
- <dependency>
- <groupId>org.json</groupId>
- <artifactId>json</artifactId>
- <version>20090211</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- </dependency>
- </dependencies>
- </project>
- /*
- some jsp file
- */
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- </head>
- <body>
- <form action="Hello1.do">
- <p> wprowadz imie:</p>
- <input type="text" name="imie" />
- <input type="submit" name="OK" />
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement