HttpServlets请求

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);
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值