zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    启动一个线程是用run()还是start()? .
    多线程有几种实现方法?同步有几种实现方法?
    同步和异步有何异同,在什么情况下分别使用他们?举例说明。
    abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
    写clone()方法时,通常都有一行代码,是什么?
    解释Spring支持的几种bean的作用域。
    Spring结构?
    说说hibernate的三种状态之间如何转换?
    测试用例设计的原则是什么?目前主要的测试用例设计方法有哪些?
    一套完整的测试应该由哪些阶段组成?
  • 原文地址:https://www.cnblogs.com/smartisn/p/14703751.html
Copyright © 2011-2022 走看看