Web - Servlet

Project Folder Structure

myWebApp/
  WEB-INF/
    web.xml
    weblogic.xml
    lib/
      MyLib.jar
    classes/
      MyPackage/
        MyServlet.class
  jsp/
  scripts/

Server

servlet_server

Web Server

A Web server exclusively handles HTTP requests. 主要负责收发Web请求,做一些简单的处理逻辑,复杂的逻辑转发给Application Server。

Java Web Server

Application Server

An application server serves business logic to application programs through any number of protocols. 主要负责复杂的business逻辑处理。

Java App Server

Servlets

Servlet is a small Java program that runs within a web server.

servlet_servlet

Servlet Life Cycle

很简单:初始化->工作中->销毁

servlet_life_cycle

Servlet Thread Safe Issue

Servlet的生命周期是由Web容器控制的,而Web容器一般会创造一个线程池,new多个Servlet。有request就从池子里拿Servlet出来处理。

所以,如果代码写的不好,是有可能发生线程安全问题的。

-> 参考 servlet线程安全问题的详解 https://blog.csdn.net/after_you/article/details/54173012

Java Class Structure

javax.servlet
     |
Servlet - Super interface for Java Servlet API
ServletContext - Provides runtime env for servlet
ServletConfig - Provides info about parameters
ServletRequest - super interface for all the requset objects
ServletResponse - super interface for all the response objects
GenericServlet - Non-http based servlets creation

javax.servlet.http
     |
HttpServletRequest
     - setAttribute()
     - getAttribute()
     - getSession()
     - getParameter()
     - getParameterValues()
     - getInitParameter()
     - getInitParameterNames()
HttpServletReponse
     - setContentType()
     - sendRedirect()
HttpServlet (class)
     - public void init()
     - public void service(HttpServletRequest, HttpServletResponse)
     - public void destroy()
     - public void doGet(HttpServletRequest, HttpServletResponse)
     - public void doPost(HttpServletRequest, HttpServletResponse)
HttpSession
     - setAttribute()
     - getAttribute()
     - getId()
     - getLastAccessedTime()
Cookie

Example

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
 
   private String message;

   public void init() throws ServletException {
      // Do required initialization
      message = "Hello World";
   }

   public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      
      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
   }

   public void destroy() {
      // do nothing.
   }
}

Quick Run Servlets in Tomcat

简单版本的,就是把classpath设好,把class文件放好,把xml改好,然后就可以run了。

Set the classpath for servlet.jar from tomcat folder

Copy the servlet .class file to the tomcat server folder

Register the servlet in web.xml

<servlet>
	<servlet-name>HelloServlet</servlet-name>
	<servlet-class>HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>HelloServlet</servlet-name>
	<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>

Start the tomcat in cmd

Open the browser and visit the URL

Session

How to Share Data Between Multiple Requests

用户的发来多个请求,服务器应该有一个上下文Context的概念。这其实就是 “会话” Session 的功能。

Cookies v.s. Session

http://stackoverflow.com/questions/359434/differences-between-cookies-and-sessions

Fork me on GitHub