用servlet演示处理客户端提交的信息

2025-11-01 20:23:06

1、先建一个项目名称为FirstServlet,然后在项目选中FirstServlet点右键->宙物新建->HTML->login    

                     

2、login.html代码如下:<html>  <head>      <title>提交表单数据</title>      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  </head>  <body bgcolor="#FFFFFF">      <h1 align="center"><b>欢迎登陆系统</b></h1>      <亲欠搁form action="getpostdata" method ="post">                    <table width="52%" border="2" align="center">              <tr bgcolor="#FFFFCC">                  <td align="center

3、保存上面代码并发布,在IE中输入“http://localhost:8080/FirstServlet/login.html”结果就显示登陆界面了

4、然后再生成请求的servlet,项目->FirstServlet点右键->新建->servlet->类名 ->GetPostDtda->包->com.servlet->完成  

5、代码如下:out.println(          "<BODY BGCOLOR=\"#FDF5E6\">\n" +           "<H1 ALIGN=CENTER>" + "get post data" + "</H1>\n"+           "<UL>\n"+           "<LI><B>username</B>:"            + request.getParameter("username") + "/n" +           "<LI><B>password</B>:"             + request.getParameter("password") + "/n"+            "</UL>\n"+               膨率  "</BODY> </HTML>" );                      out.close();   }       protected void doGet(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {      processRequest(request ,response);       }      protected void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException {      processRequest(request ,response);}      public String getServletInfo(){       return"Short description";   }

   

6、保存代码后,重新发布web,重新打开IE,得到登陆界面后用户名中输入jake,密码123,提交后就ok了!

7、另外如果出现中文乱码的情况,使用request.setCharacterEncoding()

public class HelloServlet extends HttpServlet {

 

                   

 

  public void doGet(HttpServletRequest request, HttpServletResponse

 

                                 response)   throws ServletException, IOException {

 

      request.setCharacterEncoding(“GBK”);

 

      response.setContentType("text/html;charset=GBK");//设定中文

 

      PrintWriter out = response.getWriter(); //使用输出流,输出信息

 

  }      

 

}

 

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢