创建属于自己的第一个接口
程序员文章站
2022-06-01 16:57:49
...
右键src下的包名-》new -》servlet 创建一个HelloServlet.java
HelloServlet.java
package com.yxj.testweb1;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Servlet 生命周期可被定义为从创建直到毁灭的整个过程。以下是 Servlet 遵循的过程:
Servlet 通过调用 init () 方法进行初始化。
Servlet 调用 service() 方法来处理客户端的请求。
Servlet 通过调用 destroy() 方法终止(结束)。
最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。
*
*/
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
System.out.println("测试init");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(req, resp);
System.out.println("测试service");
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("GBK");
String name = request.getParameter("name");
Enumeration<String> msg = request.getParameterNames();
//所有请求参数都是以字符串数组获取
Map<String, String[]> map = request.getParameterMap();
//获取指定字段参数值 以数组形式获取
String[] a = request.getParameterValues("name");
System.out.println(new Date()+"--->"+new Gson().toJson(map));
System.out.println(new Date()+"--->"+new Gson().toJson(a));
System.out.println(new Date()+"--->"+new Gson().toJson(msg));
if(name!=null&&name.equals("yangxiaojiang")) {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("Data", new ClassBean("java","开发语言"));
hashMap.put("msg", "登录成功!");
String dataResultString = new Gson().toJson(hashMap);
// 使用 GBK 设置中文正常显示
response.getWriter().write(dataResultString);
System.out.println(new Date()+"--->"+dataResultString);
}else {
// 使用 GBK 设置中文正常显示
response.setCharacterEncoding("GBK");
response.getWriter().write(new Date()+"--->"+"请输入正确的名字!");
System.out.println(new Date()+"--->"+"请输入正确的名字!");
}
//请求完了销毁
destroy();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
然后我们在 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">
<servlet>
<!-- 类名 -->
<servlet-name>HelloServlet</servlet-name>
<!-- 所在的包 -->
<servlet-class>com.yxj.testweb1.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<!-- 访问的网址 -->
<url-pattern>/WebApp/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
使用get请求
接口地址:http://localhost:8080/TestWeb1/WebApp/HelloServlet?name=yangxiaojiang&name=yangxiaoj&sex=0
服务器端获取上传参数log信息