HelloServlet类的doGet()方法先得到username请求参数,对其进行中文字符编码转换,然后判断username是否为空,如果为空就返回403响应状态代码,否则就通过HttpServletResponse对象的getWriter()方法得到一个PrinterWriter对象,然后通过PrintWriter对象来输出HTML文档
HelloServlet.java
package com.demo.servlet; import javax.servlet.ServletException; |
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_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>com.demo.servlet.RequestInfoServlet</servlet-class> </servlet> |
运行结果
Tomcat所在控制台结果
浏览器结果:
原文地址:https://www.cnblogs.com/sinceForever/p/8454376.html