白桦的天空

第一次的心动,永远的心痛!
  首页  :: 新随笔  :: 联系 :: 管理

HttpServlet一个典型的例子-没有乱码

Posted on 2008-05-04 10:45  白桦的天空  阅读(2350)  评论(0编辑  收藏  举报

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
    }


}