🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
![](https://box.kancloud.cn/b04d97a0f542f7d2e96a00bce7afc7f3_496x209.png) ![](https://box.kancloud.cn/cb36b35b039ad2dc82bcf91f9aea1c27_504x218.png) HelloServlet.java ~~~ package zyw.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet extends HttpServlet { @Override public void init() throws ServletException { super.init(); ServletConfig servletConfig=this.getServletConfig(); String encoding = servletConfig.getInitParameter("encoding"); System.out.println("encoding="+encoding); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //System.out.println("接收到get请求"); //获取请求行 /* System.out.println("请求方式:"+req.getMethod()); System.out.println("URI:"+req.getRequestURI()); System.out.println("发出请求客户端IP地址:"+req.getRemoteAddr()); System.out.println("服务点接收请求的IP地址:"+req.getLocalAddr()); System.out.println("访问客户端的端口号:"+req.getRemotePort()); System.out.println("web应用路径:"+req.getContextPath()); System.out.println("http协议和版本:"+req.getProtocol());*/ //获取请求头 // Enumeration枚举类型 /* Enumeration<String> headerNames = req.getHeaderNames();//自动补全变量名称 : Ctrl + Alt + v while (headerNames.hasMoreElements()){ String element = headerNames.nextElement(); System.out.println(element+":"+req.getHeader(element)); }*/ //获取请求参数 /* String name = req.getParameter("name"); String passWord = req.getParameter("passWord"); System.out.println("用户名:"+name); System.out.println("密码:"+passWord); String result="恭喜您登录成功";*/ //方法1-getOutputStream(); /*ServletOutputStream OutputStream =resp.getOutputStream(); OutputStream.write(result.getBytes());*/ //方法2-getWriter(); /* resp.setContentType("text/html;charset=utf-8");//指定输出编码 PrintWriter writer = resp.getWriter(); writer.write(result);//输出的就是字符流*/ } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("接收到post请求"); } } ~~~ web.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>zyw.servlet.HelloServlet</servlet-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping> </web-app> ~~~ ![](https://box.kancloud.cn/30be10af34bcb9c038b55b4643927835_587x50.png)