zoukankan      html  css  js  c++  java
  • HttpServlet一个典型的例子-没有乱码

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    public class mytest extends HttpServlet {

     /**
      * Constructor of the object.
      */
     private static final long serialVersionUID = 1L;
     //static final long serialVersionUID = -3387516993124229948L;
     public mytest() {
      super();
     }

     /**
      * Destruction of the servlet. <br>
      */
     public void destroy() {
      super.destroy(); // Just puts "destroy" string in log
      // Put your code here
     }

     /**
      * The doGet method of the servlet. <br>
      *
      * This method is called when a form has its tag value method equals to get.
      *
      * @param request the request send by the client to the server
      * @param response the response send by the server to the client
      * @throws ServletException if an error occurred
      * @throws IOException if an error occurred
      */
     public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {

      response.setContentType("text/html;charset=gb2312");
      PrintWriter out = response.getWriter();
      out
        .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      out.println("<HTML>");
      out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />");
      out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
      out.println("  <BODY>");
      out.print("    This is ");
      out.println(", using the GET method");
      //request.setCharacterEncoding("UTF-8");
        String title = request.getParameter("key");
        if(title==null)
        {
         title = "没有值";   
        }
      out.print(this.getClass()+title);
      out.print("<br>中文测试");
      out.println("  </BODY>");
      out.println("</HTML>");
      out.flush();
      out.close();
     }

     /**
      * The doPost method of the servlet. <br>
      *
      * This method is called when a form has its tag value method equals to post.
      *
      * @param request the request send by the client to the server
      * @param response the response send by the server to the client
      * @throws ServletException if an error occurred
      * @throws IOException if an error occurred
      */
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {

      response.setContentType("text/html;charset=gb2312");
      PrintWriter out = response.getWriter();
      out
        .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      out.println("<HTML>");
      out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
      out.println("  <BODY>");
      out.print("    This is ");
      String title = request.getParameter("key");
        if(title==null)
        {
         title = "没有值";   
        }
      title =new String(title.getBytes("ISO8859-1"),"GB2312");    
      out.print(this.getClass()+title);
      out.print("<br>中文测试");
      out.println(", using the POST method");
      out.println("  </BODY>");
      out.println("</HTML>");
      out.flush();
      out.close();
     }

     /**
      * Initialization of the servlet. <br>
      *
      * @throws ServletException if an error occure
      */
     public void init() throws ServletException {
      // Put your code here
     }

    }

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.*;


    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    public class mytest extends HttpServlet {

        
    /**
         * Constructor of the object.
         
    */

        
    private static final long serialVersionUID = 1L;
        
    //static final long serialVersionUID = -3387516993124229948L;
        public mytest() {
            
    super();
        }


        
    /**
         * Destruction of the servlet. <br>
         
    */

        
    public void destroy() {
            
    super.destroy(); // Just puts "destroy" string in log
            
    // Put your code here
        }


        
    /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         * 
         * 
    @param request the request send by the client to the server
         * 
    @param response the response send by the server to the client
         * 
    @throws ServletException if an error occurred
         * 
    @throws IOException if an error occurred
         
    */

        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html;charset=gb2312");
            PrintWriter out 
    = response.getWriter();
            out
                    .println(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(
    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />");
            out.println(
    "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(
    "  <BODY>");
            out.print(
    "    This is ");
            out.println(
    ", using the GET method");
            
    //request.setCharacterEncoding("UTF-8"); 
              String title = request.getParameter("key");
              
    if(title==null)
              
    {
                  title 
    = "没有值";          
              }

            out.print(
    this.getClass()+title);
            out.print(
    "<br>下面显示的是读取文件的内容:");
            File f 
    = new File("D:\\java\\test.html");
            InputStreamReader read 
    = new InputStreamReader (new FileInputStream(f),"GB2312");
            BufferedReader reader
    =new BufferedReader(read);
            String line;
            
    while ((line = reader.readLine()) != null{
            out.println(line);
            }

            out.println(
    "  </BODY>");
            out.println(
    "</HTML>");
            out.flush();
            out.close();
        }


        
    /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         * 
         * 
    @param request the request send by the client to the server
         * 
    @param response the response send by the server to the client
         * 
    @throws ServletException if an error occurred
         * 
    @throws IOException if an error occurred
         
    */

        
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html;charset=gb2312");
            PrintWriter out 
    = response.getWriter();
            out
                    .println(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(
    "  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(
    "  <BODY>");
            out.print(
    "    This is ");
            String title 
    = request.getParameter("key");
              
    if(title==null)
              
    {
                  title 
    = "没有值";          
              }

            title 
    =new String(title.getBytes("ISO8859-1"),"GB2312");           
            out.print(
    this.getClass()+title);
            out.print(
    "<br>中文测试");
            out.println(
    ", using the POST method");
            out.println(
    "  </BODY>");
            out.println(
    "</HTML>");
            out.flush();
            out.close();
        }


        
    /**
         * Initialization of the servlet. <br>
         *
         * 
    @throws ServletException if an error occure
         
    */

        
    public void init() throws ServletException {
            
    // Put your code here
        }


    }

  • 相关阅读:
    leetcode刷题笔记 217题 存在重复元素
    leetcode刷题笔记 二百零六题 反转链表
    leetcode刷题笔记 二百零五题 同构字符串
    20201119日报
    np.percentile 和df.quantile 分位数
    建模技巧
    np.where() 条件索引和SQL的if用法一样,或者是给出满足条件的坐标集合
    np.triu_indices_from() 返回方阵的上三角矩阵的索引
    ax.set_title() 和 plt.title(),以及df,plot(title='')
    信用卡模型(三)
  • 原文地址:https://www.cnblogs.com/chinatefl/p/1181363.html
Copyright © 2011-2022 走看看