1.文件
2.页面联系
addStudent.jsp是学生信息添加页面,当信息填写完毕点击提交按钮时,AddStudentServlet处理用户请求,处理完毕后跳转到message1.jsp页面。
AddStudentServlet.java中代码为:
package ch01;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddStudentServlet extends HttpServlet{
protected void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String studentNumber=request.getParameter("studentNumber");
String studentName=request.getParameter("studentName");
String studentSex=request.getParameter("studentSex");
String studentAge=request.getParameter("studentAge");
String studentWeight=request.getParameter("studentWeight");
DBJavaBean db=new DBJavaBean();
if(db.addStudent(studentNumber, studentName, studentSex, studentAge, studentWeight)){
response.sendRedirect("./code/myCode/studentManage/message1.jsp");
}
else{
response.sendRedirect("./code/myCode/studentManage/addStudent.jsp");
}
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doGet(request,response);
}
}
在web.xml中对servlet进行配置:
<?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">
<display-name>TomcatTest</display-name>
<welcome-file-list>
<welcome-file>stuAdmin.jsp</welcome-file>
<welcome-file>left.jsp</welcome-file>
<welcome-file>top.jsp</welcome-file>
<welcome-file>bottom.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AddStudentServlet</servlet-name>
<servlet-class>ch01.AddStudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddStudentServlet</servlet-name>
<url-pattern>/AddStudentServlet</url-pattern>
</servlet-mapping>
</web-app>
两个servlet-name应相同,action里的指向应与url-pattern内容中/后面的相同。因此
jsp中的action="AddStudentServlet",但是会发生404错误:
是因为路径不对,AddStudentServlet是放在java Resources中的ch01包中的,而不是在studentManage下的。所以应加上项目名action="/TomcatTest/AddStudentServlet"
在addStudent.jsp中代码为:
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生信息添加</title>
</head>
<body bgcolor="CCCFFF">
<center>
<br><br><br>
<h3> 添加学生信息</h3>
<form action="/TomcatTest/AddStudentServlet" method="get">
<table border="1" width="230">
<tr>
<td>学号:</td>
<td><input type="text" name="studentNumber" /></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="studentName" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="studentSex" /></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="studentAge" /></td>
</tr>
<tr>
<td>体重:</td>
<td><input type="text" name="studentWeight" /></td>
</tr>
<tr align="center">
<td>
<input type="submit" name="sure" value="提交" />
<input type="reset" name="clear" value="取消" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>