地址栏输入信息返回响应结果
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>unit8</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Servlet3</servlet-name>
<servlet-class>com.unit8.servlet.Servlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet3</servlet-name>
<url-pattern>/Servlet3</url-pattern>
</servlet-mapping>
</web-app>
<!-- 核心配置文件 -->
package com.unit8.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet3 extends HttpServlet {
// 处理 get 请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("GBK");//设置请求数据的编码
response.setCharacterEncoding("GBK");//设置相应数据的编码
String s = request.getParameter("name");
PrintWriter pw = response.getWriter();
pw.print("您输入的 name 值是"+s);
}
// 处理 post 请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
注意事项:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qjhcWe16-1618809901919)(D:\desktop\MarkDown学习\新建文件夹\图片\中文编码\中文编码方式01.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dUoCAlMU-1618809901923)(D:\desktop\MarkDown学习\新建文件夹\图片\中文编码\中文编码方式03.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VEirXjWb-1618809901924)(D:\desktop\MarkDown学习\新建文件夹\图片\中文编码\中文编码方式02.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JyrS8P1j-1618809901926)(D:\desktop\MarkDown学习\新建文件夹\图片\中文编码\中文编码 name=张三.png)]