<servlet>
<!-- <description>描述这个servlet的信息(这句可有可无)</description>-->
<servlet-name>Delete_General_Question</servlet-name>
<servlet-class>hhstu.bysj.servletDelete.Delete_General_Question</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Delete_General_Question</servlet-name>
<url-pattern>/servlet/Delete_General_Question</url-pattern>
</servlet-mapping>
这种格式一定要记清楚
1、servlet和servlet-mapping标签下<servlet-name>的内容是一样的
2、 <servlet-class>这个servlet调用的类名,这个类名一定要写的非常完整(就是包含包名)
3、<url-pattern>调用servlet的时候要用到的地址建议和类名相同,比如在提交表单的时候<form action = "servlet/Delete_General_Question" method="post">
这个时候,以post的方式提交表单,调整至Delete_General_Question这个servlet,让其处理
下面举一个servlet的实例
public class Delete_Appli_Question extends HttpServlet {
public Delete_Appli_Question() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = null;
boolean flag =false;
request.setCharacterEncoding("utf-8");
String questionId = (String)request.getParameter("questionId").trim();
try {
int id_temp = Integer.parseInt(questionId);
flag = DaoFactory.getAppli_QuestionDao().doDelete(id_temp);
if (flag) {
request.setAttribute("info", "应用题删除成功");
dispatcher = request.getRequestDispatcher("/success.jsp");
dispatcher.forward(request, response);
return;
} else {
dispatcher = request.getRequestDispatcher("/delete_appli_question.jsp");
dispatcher.forward(request, response);
return;
}
} catch (NumberFormatException e) {
dispatcher = request.getRequestDispatcher("/delete_appli_question.jsp");
dispatcher.forward(request, response);
return;
} catch (Exception e) {
dispatcher = request.getRequestDispatcher("/delete_appli_question.jsp");
dispatcher.forward(request, response);
return;
}
}
public void init() throws ServletException {
}
}
|