标签:round 生命周期 use web服务 nbsp prot image col 并且
Servlet是在服务器端运行的一个小程序,实际上一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问的这个驻留在服务器内
存里的servlet程序。主要用来处理客户端请求、响应给浏览器的动态资源
Web服务器在与客户端交互时.Servlet的工作原理是:
配置Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Servlet_HelloWorld</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>com.servlets.HelloWorldServlet</servlet-class> <!-- 当值为0或者大于0时,表示容器在启动时就加载并初始化这个servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Servlet处理类
package com.servlets; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; //@WebServlet("/helloworldservlet") 可以在Web.xml中配置servlet代替 public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; public HelloWorldServlet() { super(); System.out.println("创建Servlet实例"); } public void init(ServletConfig config) throws ServletException { System.out.println("初始化"); } public void destroy() { System.out.println("销毁"); } protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("调用service()方法"); response.setCharacterEncoding("UTF-8"); //获得参数 String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); //将数据存入Session HttpSession session = request.getSession(); session.setAttribute("userName", userName); session.setAttribute("userPwd", userPwd); //转发 request.getRequestDispatcher("/success.jsp").forward(request, response); } }
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>First Servlet</title> </head> <body> <a href="${pageContext.request.contextPath}/helloworldservlet.do?userName=张三&userPwd=123456">HelloWorld!</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>${userName}</h1> <h1>${userPwd}</h1> </body> </html>
注意:若是service()、doPost()和doGet()这三个方法都在存在的情况下,java只会执行service()方法,而其他的两种方法不会被执行。若是没有service() 方法,则是根据jsp传入方式选择对应的方法
标签:round 生命周期 use web服务 nbsp prot image col 并且
原文地址:https://www.cnblogs.com/fengfuwanliu/p/10512689.html