1.继承HttpServlet
2.重写doPost()方法。
3.如果需要获取上下文 则需要获取上下文内容
4.配置web.xml
<!-- 对继承自HttpServlet的类进行调用时,必须在此配置 -->
<servlet>
<!-- 对HttpServlets.java设置别名,叫UpdateGlDeptId -->
<servlet-name>UpdateGlDeptId</servlet-name>
<servlet-class>com.ssh.user.action.HttpServlets</servlet-class>
</servlet>
<!-- 对HttpServlets的别名UpdateGlDeptId进行映射 -->
<servlet-mapping>
<servlet-name>UpdateGlDeptId</servlet-name>
<!--
在地址栏通过http://localhost/myWeb/changes
或者http://localhost/FirstWeb/changes来调用MyFirstServlet.java -->
<url-pattern>/changes</url-pattern>
</servlet-mapping>
<!-- <servlet-mapping>
<servlet-name>UpdateGlDeptId</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> -->
5.如果项目自动跳转到登录页面需要配置该servlet可以直接访问
本项目是在applicationContext-security.xml 中配置
<http pattern="/changes" security="none" />即可
servlet实现类如下
package com.ssh.user.action;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.context.ApplicationContext;
import com.ssh.common.util.SpringContextUtils;
import com.ssh.user.service.ZzjgbService;
import java.util.*;
// Extend HttpServlet class
public class HttpServlets extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
// Method to handle GET method request.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type - by www.yiibai.com
String cdptcde9 = request.getParameter("cdptcde9");
String gldeptid =request.getParameter("gldeptid");
Map<String,String> map = new HashMap<String, String>();
map.put("cdptcde9", cdptcde9);
map.put("gldeptid", gldeptid);
//获取上下文加载的service对服务进行处理
ApplicationContext applicationContext =SpringContextUtils.getApplicationContext();ZzjgbService zzjgbService = (ZzjgbService) applicationContext.getBean("zzjgbService");
//zzjgbService.getZzjgByCode9(cdptCde9first5);
int result = zzjgbService.updateGldeptId(map);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if(result==0){
out.println("该业务编码不存在(9字头编码不存在)");
}else{
out.println("修改成功");
}
/*String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";*/
/*Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getHeader(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
out.println("</table>\n</body></html>");*/
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}