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标签包含名字(可以自己起名)和继承HttpServlet类路径-->
<servlet>
<servlet-name>haha</servlet-name>
<servlet-class>com.example.demo.First</servlet-class>
</servlet>
<!-- 配置servlet-mapping标签包含名字(和servlet标签名字一致)和url-pattern(form表单action值一样)-->
<servlet-mapping>
<servlet-name>haha</servlet-name>
<url-pattern>/zhuce</url-pattern>
</servlet-mapping>
</web-app>
点击查看代码
public class First extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
// UserDao userDao = new UserDao();
// int t = userDao.save(username,password);
// System.out.println(t);
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
UserDao userDao = new UserDao();
List<User> list = userDao.selectById(password);
for (User us : list) {
writer.println("password:"+" " + us.getPassword()+" "+"username:"+" "+us.getUsername()+"</br>");
}
}
}