标签:utf-8 功能 jsp 需要 creat dog 运行 解析 mat
一、概述
Servlet是使用Java编写的服务端程序,主要功能是交互式的浏览和修改数据,从而生成动态的Web内容。
Servlet运行需要特定的容器,例如由Tomcat为Servlet提供基本运行环境。
当Web服务器接收到HTTP请求时,会将该请求交给Servlet容器。容器首先对请求的URL进行解析,并根据web.xml找到相应的Servlet,如果该Servlet为第一次访问,则调用则初始化之。之后将request和respone传递给该Servlet。Servlet通过request获取客户端的请求信息,处理完成后将返回的信息放入respone。之后控制权回到Web服务器,返回HTTP响应到客户端。
Servlet的状态由其容器来维护,生命周期从服务器运行开始直至关闭,包括加载、初始化、运行和销毁阶段。
二、常用接口及类
三、创建与调用
//MyServlet.java import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("GB2312"); PrintWriter out = resp.getWriter(); String content = "<DOCTYPE html>" + "<html>" + "<head>" + "<title>servlet</title>" + "</head>" + "<body>" + "servlet has been created and configured" + "</body>" + "</html>"; out.println(content); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } //web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> </web-app>
获取Servlet信息。
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); PrintWriter out = resp.getWriter(); //获取HTTP头部信息 Enumeration enumeration = req.getHeaderNames(); while (enumeration.hasMoreElements()) { String name = (String) enumeration.nextElement(); String value = req.getHeader(name); out.println(String.format("%s = %s<br/>", name, value)); } //获取请求对象信息 out.println(req.getMethod() + "<br/>"); out.println(req.getProtocol() + "<br/>"); out.println(req.getRemoteAddr() + "<br/>"); out.println(req.getRequestURL() + "<br/>"); out.println(req.getRequestURI() + "<br/>"); //获取参数信息 out.println(req.getParameter("user") + "<br/>"); out.println(req.getParameter("pass") + "<br/>"); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
JSP调用Servlet。
//方法一 <form action="MyServlet" method="post"> ... </form> //方法二 <a href="MyServlet?user=root&pass=0000">Call MyServlet</a>
标签:utf-8 功能 jsp 需要 creat dog 运行 解析 mat
原文地址:https://www.cnblogs.com/arseneyao/p/9323428.html