使用Eclipse创建一个简单的servlet项目(账号密码)

Posted 张家欢。。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Eclipse创建一个简单的servlet项目(账号密码)相关的知识,希望对你有一定的参考价值。

1.创建一个servlet

 1 package com.huawei.jiekou;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class TestServlet
12  */
13 @WebServlet(description = "???????servlet", urlPatterns = { "/TestServlet" })
14 public class dengru extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16        
17     /**
18      * @see HttpServlet#HttpServlet()
19      */
20     public dengru() {
21         super();
22         // TODO Auto-generated constructor stub
23     }
24 
25     /**
26      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
27      */
28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         String name = request.getParameter("name");
31         String sex = request.getParameter("sex");
32         String age = request.getParameter("age");
33         String str = "{\"name\":\"" + name + "\",\"sex\":\"" + sex + "\",\"age\":" + age + "}";
34         response.getWriter().write(str);
35 //        response.getWriter().append("Served at: ").append(request.getContextPath());
36     }
37 
38     /**
39      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
40      */
41     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42         // TODO Auto-generated method stub
43 //        doGet(request, response);
44         String account = request.getParameter("account");
45         String psd = request.getParameter("psd");
46         if(account.equals("admin") && psd.equals("123456")) {
47             request.getRequestDispatcher("test1.jsp").forward(request, response);
48         }else if(account.equals("admin") && !psd.equals("123456")) {
49             request.setAttribute("psdErrorString", "password is error");
50             request.getRequestDispatcher("test.jsp").forward(request, response);
51         }else if(!account.equals("admin") && psd.equals("123456")) {
52             request.setAttribute("accountErrorString", "account is error");
53             request.getRequestDispatcher("test.jsp").forward(request, response);
54         }else {
55             request.setAttribute("errorString", "login error");
56             request.getRequestDispatcher("test.jsp").forward(request, response);
57         }
58     }
59 
60 }

创建jsp

 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="TestServlet" method="post">
11     账号:<input type="text" name="account" />${accountErrorString }<br>
12     密码:<input type="password" name="psd" />${psdErrorString }<br>
13     <input type="submit" value="提交" /><br>${errorString }
14 </form>
15 
16 </body>
17 </html>
 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 <h1>登录成功</h1>
11 </body>
12 </html>

 

以上是关于使用Eclipse创建一个简单的servlet项目(账号密码)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用eclipse创建简单的servlet

如何使用eclipse创建简单的servlet

如何在eclipse中创建一个servlet程序

如何在Eclipse中创建web项目并使用tomcat8 运行servlet开发简单的动态网页?

使用eclipse创建一个简单的maven项目

如何使用eclipse创建简单的servlet