分享

一个完整的简单jsp+servlet实例,实现简单的登录

 春和秋荣 2020-02-22

1、先创建web project,项目名为RegisterSystem,

2、在WebRoot 目录下创建login.jsp文件:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'login.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. This is my JSP page. <br>
  22. <form action="login">
  23. username:<input type="text" name="username"><br>
  24. password:<input type="password" name="pwd"><br>
  25. <input type="submit">
  26. </form>
  27. </body>
  28. </html>


3、在scr目录下的com.ht.servlet编写AccountBean.java文件,代码如下:

package com.ht.servlet;

public class AccountBean {
 private String username;
 private String password;
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
}

4、在scr目录下的com.ht.servlet编写servlet类CheckAccount.java文件,代码如下:

package com.ht.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CheckAccount extends HttpServlet {

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGet(req,resp);
 }

 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  HttpSession session = req.getSession();
  AccountBean account = new AccountBean();
  String username = req.getParameter("username");
  String pwd = req.getParameter("pwd");
  account.setPassword(pwd);
  account.setUsername(username);
  if((username != null)&&(username.trim().equals("jsp"))) {
   if((pwd != null)&&(pwd.trim().equals("1"))) {
    System.out.println("success");
    session.setAttribute("account", account);
    String login_suc = "success.jsp";
    resp.sendRedirect(login_suc);
    return;
   }
  }
  String login_fail = "fail.jsp";
  resp.sendRedirect(login_fail);
  return;
 }
 
}
5、在WebRoot目录下编写success.jsp文件 成功后跳转

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@page import="com.ht.servlet.AccountBean"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'success.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. <%
  23. AccountBean account = (AccountBean)session.getAttribute("account");
  24. %>
  25. username:<%= account.getUsername()%>
  26. <br>
  27. password:<%= account.getPassword() %>
  28. basePath: <%=basePath%>
  29. path:<%=path%>
  30. </body>
  31. </html>

6、在WebRoot目录下编写fail.jsp文件 失败后跳转

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'fail.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. Login Failed! <br>
  22. basePath: <%=basePath%>
  23. path:<%=path%>
  24. </body>
  25. </html>

7、修改web.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java./xml/ns/javaee"
  4. xmlns:xsi="http://www./2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java./xml/ns/javaee
  6. http://java./xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>login.jsp</welcome-file>
  9. </welcome-file-list>
  10. <servlet>
  11. <description>This is the description of my J2EE component</description>
  12. <display-name>This is the display name of my J2EE component</display-name>
  13. <servlet-name>CheckAccount</servlet-name>
  14. <servlet-class>com.ht.servlet.CheckAccount</servlet-class>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>CheckAccount</servlet-name>
  18. <url-pattern>/login</url-pattern>
  19. </servlet-mapping>
  20. </web-app>

输入测试路径:http://localhost:8080/RegisterSystem/login.jsp

注意:

.处理中文乱码问题

在Sertlet中加

response.setContentType("text/html;charset=utf-8")

在jsp页面中加

<%@ page language="java" import="java.util.*,java.net.URLEncoder" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

在servlet中

String username=request.getParameter("username");

username=new String(username.getBytes(“ISO-8859-1”),"UTF-8");


reference :http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html

http://lelglin./blog/967503

http://zhidao.baidu.com/link?url=qg1AcsUjhkG4gHBv8GtHDjIQuwgtLto1_eyeinBx-b8TW9HAcYRR1zizL8vTDPTAjLwHPOdz7NcwJun-HyJXeK


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多