代码拉取完成,页面将自动刷新
(1)JSP索引页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="servlet/addUser.action" method="post">
姓名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>
(2)业务逻辑代码UserManager
package edu.eurasia;
import java.util.ArrayList;
import java.util.List;
public class UserManager {
public void addUser(String username) {
System.out.println("UserManager.addUser()--->username:" + username);
}
public void delUser(String username) {
System.out.println("UserManager.delUser()--->username:" + username);
}
public void modifyUser(String username) {
System.out.println("UserManager.modifyUser()--->username" + username);
}
public List queryUser(String username) {
System.out.println("UserManager.queryUser()--->username" + username);
List userList = new ArrayList();
userList.add("a");
userList.add("b");
userList.add("c");
return userList;
}
}
(3)Servlet控制代码
package edu.eurasia;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@WebServlet(urlPatterns={"*.action"})
@WebServlet("/servlet/addUser.action")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String requestURI = request.getRequestURI();//获取该页面的相对路径
System.out.println("requestURI=" + requestURI);
String path = requestURI.substring(requestURI.indexOf("/", 1),
requestURI.indexOf("."));
System.out.println("path=" + path);
String username = request.getParameter("username");
UserManager userManager = new UserManager();
String forward = "";
if ("/servlet/delUser".equals(path)) {
userManager.delUser(username);
forward = "/del_success.jsp";
} else if ("/servlet/addUser".equals(path)) {
userManager.addUser(username);
forward = "/add_success.jsp";
} else if ("/servlet/modifyUser".equals(path)) {
userManager.modifyUser(username);
forward = "/modify_success.jsp";
} else if ("/servlet/queryUser".equals(path)) {
List userList = userManager.queryUser(username);
request.setAttribute("userList", userList);
forward = "/query_success.jsp";
} else {
throw new RuntimeException("请求失败");
}
request.setAttribute("u", username);
request.getRequestDispatcher(forward).forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
注解@WebServlet用于在Web应用程序中标记一个继承了HttpServlet的类为Servlet。其属性urlPatterns的值即是web.xml中的url-pattern。
request.getRequestDispatcher()是请求转发,前后页面共享一个request.
response.sendRedirect()是重新定向 前后页面不是一个request.
控制台输出:
/Day02_01/servlet/addUser.action
path=/servlet/addUser
UserManager.addUser()--->username:张三疯
(4)add_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>addUser
<%=request.getAttribute("u") %>
<%-- <%=request.getParameter("username") %> --%>
${requestScope.u}
success!</h1>
</body>
</html>
这个servlet代码主要实现的功能判断是那个页面请求服务器做那些操作,之后调用业务逻辑实现相应业务操作。
(5)配置Servlet
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>strut2_1</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>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>edu.eurasia.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。