此方法用于在对Javaee开发中的通过对应的名称而选择servlet中的对应的方法
注:主要代码如下
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 这是设置浏览器编码的问题
*/
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html,charset=UTF-8");
String servletPath = req.getServletPath();
String methodName = servletPath.substring(1);
methodName = methodName.substring(0, methodName.length()-4);
System.out.println(methodName);
try {
/**
* 根据反射机制选择servlet对应的方法
*/
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class);
/**
* 利用反射调用对应的方法
*/
method.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
System.out.println("这里是登录的时候反射机制的地方出错了!");
req.getRequestDispatcher("error.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
示例
注:在后面的jsp页面中提交form表单中的 action="<%=request.getContextPath() %>/saveTeacher.ado"可以直接通过反射机制调用servlet中的saveTeacher方法
public class AdministratorsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 这是设置浏览器编码的问题
*/
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html,charset=UTF-8");
String servletPath = req.getServletPath();
String methodName = servletPath.substring(1);
methodName = methodName.substring(0, methodName.length()-4);
System.out.println(methodName);
try {
/**
* 根据反射机制选择servlet对应的方法
*/
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class);
/**
* 利用反射调用对应的方法
*/
method.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
System.out.println("这里是登录的时候反射机制的地方出错了!");
req.getRequestDispatcher("error.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
/**
* 基于对教师的基本操作
*/
/**
* 添加一个教师
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
private void saveTeacher(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Teachers teacher = new Teachers();
teacher.setTeacherId(Integer.parseInt(req.getParameter("teacherId")));
teacher.setTeacherName(req.getParameter("teacherName"));
teacher.setSex(req.getParameter("sex"));
teacher.setPhoneNo(req.getParameter("phoneNo"));
teacher.setAddress(req.getParameter("address"));
teacher.setBirthday(new Date(new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("birthday")).getTime()));
teacher.setSalary(new BigDecimal(req.getParameter("salary")));
ITeachersService teacherService = new TeachersServiceImpl();
int rows = teacherService.save(teacher);
if(rows>0) {
System.out.println("保存一个教师基本信息成功!");
req.getRequestDispatcher("saveSuccess.jsp").forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("添加一个教师对象基本信息的时候失败!");
resp.sendRedirect("error.jsp");
}
}
}
一、首先在web.xml文件里面配置相关的信息如下
注: <url-pattern>*.ado</url-pattern> 中的*则代表所选的对应方法
<servlet>
<servlet-name>AdministratorsServlet</servlet-name>
<servlet-class>cn.gbb.student_achievement_system.controller.AdministratorsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdministratorsServlet</servlet-name>
<url-pattern>*.ado</url-pattern>
</servlet-mapping>
二、在jsp页面中from表单提交的时候需要注意它提交的action里面的内容
注: <form action="<%=request.getContextPath() %>/saveTeacher.ado" method="get">
1 <body style="height: 100%;width: 100%; background-image:url(lib/skr.jpg); background-size: cover;">
2 <div style="height: 100px;width: 100%; text-align: center;">
3 <h1>学生成绩管理系统</h1>
4 </div>
5 <div align="center">
6 <form action="<%=request.getContextPath() %>/saveTeacher.ado" method="get">
7 <%
8 Teachers_Login teacherLogin = (Teachers_Login) request.getAttribute("teacherLogin");
9 %>
10 <input type="hidden" name="teacherId" value="<%=teacherLogin.getTeacherId()%>">
11 <input type="hidden" name="teacherName" value="<%=teacherLogin.getTeacherName()%>">
12 <table>
13 <tr>
14 <th colspan="2" align="center">请填写教师的基本信息</th>
15 </tr>
16 <tr>
17 <td>职工号:</td>
18 <td><%=teacherLogin.getTeacherId()%></td>
19 </tr>
20 <tr>
21 <td>姓名:</td>
22 <td><%=teacherLogin.getTeacherName()%></td>
23 </tr>
24 <tr>
25 <td>性别:</td>
26 <td><input type="text" name="sex"></td>
27 </tr>
28 <tr>
29 <td>电话号码:</td>
30 <td><input type="text" name="phoneNo"></td>
31 </tr>
32 <tr>
33 <td>家庭住址:</td>
34 <td><input type="text" name="address"></td>
35 </tr>
36 <tr>
37 <td>出生日期:</td>
38 <td><input type="text" name="birthday"></td>
39 </tr>
40 <tr>
41 <td>工资:</td>
42 <td><input type="text" name="salary"></td>
43 </tr>
44 <tr>
45 <td colspan="2" style="text-align: center;"><input type="submit" value="确定"><input type="reset" value="重置"></td>
46 </tr>
47 </table>
48 </form>
49 <p style="text-align: center;"><a href="administratorsMain.jsp">返回首页</a></p>
50 </div>
51 </body>
总结:这样就可以在jsp页面提交的时候选择servlet里面的所对应的方法了,希望对您有所帮助,谢谢!!!!!