码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet练习

时间:2016-07-02 00:18:20      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

编写一个Servlet,注册登录成功后,讲表单中的内容输出到页面当中

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="ser" method="post">
11 用户名<input type="text" name="username">
12 <br><br>
13 密码&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password">
14 <br><br>
15 <input type="submit" value="登录">
16 </form>
17 </body>
18 </html>
 1 package com.servlet.web;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import javax.servlet.http.HttpSession;
 9 import javax.websocket.Session;
10 
11 
12 public class Servlet1 extends HttpServlet {
13     private static final long serialVersionUID = 1L;
14    
15     public Servlet1() {
16         super();
17         
18     }
19 
20     
21     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22         response.setContentType("text/html");
23         response.setCharacterEncoding("UTF-8");
24         String un=new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
25         String pw=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
26         
27         if(un!=null&&pw!=null)
28         {
29           if(!un.equals(""))
30           {
31             if(un.equals("妍希")&&pw.equals("14567"))
32             {
33                 response.getWriter().write(un+" 欢 迎  你 来 做 淄 博 游 玩!"+"<br>");
34             }
35             else
36             {
37                 response.getWriter().write("输入的账号和密码有误");
38             }
39           }
40           else
41           {
42               response.getWriter().write("账号不能为空");
43           }
44         }
45         else
46         {
47             response.getWriter().write("请正确访问");
48         }
49         response.getWriter().append("Served at: ").append(request.getContextPath());
50     }
51 
52     
53     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
54         
55         doGet(request, response);
56     }
57 
58 }

技术分享

技术分享

Servlet练习

标签:

原文地址:http://www.cnblogs.com/yg6405816/p/5634433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!