步骤
1.创建项目
2.引入jar包
3.创建Servlet
4.配置web.xml
5.部署到tomcat
6.访问API
1.创建项目MyFirstAPI
2.引入jar包
3.创建Servlet
编写Servlet代码
package com.lry.login;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username"); // 获取客户端传过来的参数
String password = request.getParameter("password");
Map<String,String> map=new HashMap<String, String>();
map.put("username", username);
map.put("password", password);
map.put("result", "0");
String mapStr=new Gson().toJson(map);
response.getWriter().write(mapStr);//返回json数据
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.配置web.xml
5.发布到Tomcat
6.访问API
http://localhost:8080/MyFirstAPI/login?username=zhangsan&password=123