IDEA 生成servlet模板文件案例

servlet模板文件:

 1 //package com.servlet;
 2 import java.io.File;
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.sql.SQLException;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15 
16 import com.alibaba.fastjson.JSONObject;
17 
18 /**
19  * Servlet implementation class Study_score
20  */
21 @WebServlet("/Servlet_example")
22 public class Servlet_example extends HttpServlet {
23     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
24         // TODO Auto-generated method stub
25         req.setCharacterEncoding("utf-8");
26         String method = req.getParameter("method");
27         if ("method_example".equals(method)) {
28                 method_example(req, resp);
29         }
30         
31         
32         
33     }   
34     public Servlet_example() {
35         super();
36         // TODO Auto-generated constructor stub
37     }
38 
39     private void method_example(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
40           // TODO Auto-generated method stub
41           //response.getWriter().append("Served at: ").append(request.getContextPath());
42           req.setCharacterEncoding("utf-8");
43           resp.setContentType("text/html;charset=utf-8");
44           HttpSession session=req.getSession();
45           PrintWriter out=resp.getWriter();
46         System.out.println("AAAAAAAAAAAAA");
47         //获取传输过来的值
48         String[] translate_example = req.getParameterValues("translate_example");
49 
50         //返回页面
51         String whole=req.getContextPath()+"/example.html";
52         String info="上传成功";
53         resp.getWriter().print("<script> var whole=""+whole+"";
" +
54                 "var r=confirm(""+info+"");
" +
55                 "if (r==true){
" +
56                 "        window.location.assign(whole);
" +
57                 "    }
" +
58                 "    else{
" +
59                 "        window.location.assign("./index.jsp");;
" +
60                 "    }    "
61                 + "</script>");
62           }
63   
64 }

 ————————————————————————————————————————————————————————————————————————————

更改:

 1 package com.servlet;//package com.servlet;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.PrintWriter;
 6 import java.sql.SQLException;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 import javax.servlet.ServletException;
11 import javax.servlet.annotation.WebServlet;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
16 
17 import com.alibaba.fastjson.JSONObject;
18 
19 /**
20  * Servlet implementation class Study_score
21  */
22 @WebServlet("/Servlet_example")
23 public class Servlet_example extends HttpServlet {
24     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
25 // TODO Auto-generated method stub
26         req.setCharacterEncoding("utf-8");
27         String method = req.getParameter("method");
28         try {
29             method_example(req, resp);
30         } catch (SQLException throwables) {
31             throwables.printStackTrace();
32         }
33     }
34 
35     public Servlet_example() {
36         super();
37         // TODO Auto-generated constructor stub
38     }
39 
40     private void method_example(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
41         // TODO Auto-generated method stub
42         //response.getWriter().append("Served at: ").append(request.getContextPath());
43         req.setCharacterEncoding("utf-8");
44         resp.setContentType("text/html;charset=utf-8");
45         HttpSession session = req.getSession();
46         PrintWriter out = resp.getWriter();
47         System.out.println("AAAAAAAAAAAAA");
48         //获取传输过来的值
49         String[] translate_example = req.getParameterValues("translate_example");
50         //返回页面
51         String whole = req.getContextPath() + "/example.html";
52         String info = "上传成功";
53         resp.getWriter().print("<script> var whole="" + whole + "";
" +
54                 "var r=confirm("" + info + "");
" +
55                 "if (r==true){
" +
56                 "        window.location.assign(whole);
" +
57                 "    }
" +
58                 "    else{
" +
59                 "        window.location.assign("./index.jsp");;
" +
60                 "    }    "
61                 + "</script>");
62     }
63 
64 }
原文地址:https://www.cnblogs.com/smartisn/p/14703751.html