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

时间:2025-04-04 18:58:33

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"> <p></p> <table width="52%" border="2" align="center"> <tr bgcolor="#FFFFCC"> <td align="center" width="43%" ><div align="center">用户名:</div></td> <td width="57%"><div align="left"> <input type="text" name="username"> </div></td> </tr> <tr bgcolor="#CCFF99"> <td align="center" width="43%"><div align="center">密码:</div></td> <td width="57%"><div align="left"> <input type="password" name="password"> </div></td> </tr> </table> <p align="center"> <input type="reset" name="Reset" value="重置"> <input type="submit" name="Submit2" value="提交"> </p> </form> </body></html>

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

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

5、代码如下:out.println( "<BODY BGCO雉搽妤粲LOR=\"#FDF5E6\">\n" + "<H1 ALIG绿覆冗猩N=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(); //使用输出流,输出信息 } }

© 2025 一点知道
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com