ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JSP学习

2021-11-13 12:59:10  阅读:111  来源: 互联网

标签:String 登录 学习 jsp nbsp JSP password 页面


session/cookie

检查页面check.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-utf-8">
<title>check</title>
</head>
<body>
<form method="post" action="">
<% 
   String id=(String)session.getAttribute("id");
   String password=(String)session.getAttribute("password");
   String name=request.getParameter("id");
   session.setAttribute("name", name);
   String password1=request.getParameter("password");
   session.setAttribute("password", password1);
if(id.equals(name)&&password1.equals(password))
{ response.sendRedirect("success.jsp");}
else
response.sendRedirect("error.jsp");

if (id.equals("linxiao") && password.equals("12138")) 
	{
    Cookie nameCookie = new Cookie("name", id);
    Cookie passwordCookie = new Cookie("password", password);
    nameCookie.setPath(request.getContextPath() + "/");
    passwordCookie.setPath(request.getContextPath() + "/");
    String rememberme = request.getParameter("rememberme");
    if (rememberme.equals("on")) 
    {
        nameCookie.setMaxAge(7 * 24 * 60 * 60)	;
        passwordCookie.setMaxAge(7 * 24 * 60 * 60);
    }
    else {
        nameCookie.setMaxAge(0);
        passwordCookie.setMaxAge(0);
    }
    response.addCookie(nameCookie);
    response.addCookie(passwordCookie);
	}
%>
</body>
</html>

错误页面error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录失败</title>
<style>
 #a {
     width: 360px;
  	 padding: 8% 0 0;
     margin: auto;
}
</style>
</head>
<body>
<div id="a">
<h1 align="center">
登录失败
</h1>
<h2 align="center">请检查用户名或密码是否正确</h2>
<h3 align="center"><a href="login.jsp" style="color:#FAAF46 font-size:10px">返回登录页面</a></h3>
</div>
</body>
</html>

登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-utf-8">
<title>登录</title>
<style>
#a {
	width: 360px;
	padding: 8% 0 0;
	margin: auto;
	}
</style>
</head>
<body>
<div id="a">
	<%
        String user = "";        
        String pass = "";        
        String checked = "";       
        Cookie[] cookies = request.getCookies();        
        if (cookies != null && cookies.length > 0) {           
            for (int i = 0; i < cookies.length; i++) {                
                Cookie cookie = cookies[i];                
                if ("name".equals(cookie.getName())) {                  
                    user = URLDecoder.decode(cookie.getValue(), "utf-8");                   
                    checked = "checked";
                }               
                if ("password".equals(cookie.getName())) {                    
                    pass = cookie.getValue();
                }
            }
        }
    %>
<h1>登录界面</h1>
<form action="check.jsp" method="post">
账号:&nbsp;&nbsp;<input type="text" name="id" value="<%=user%>" /><br><br>
密码:&nbsp;&nbsp;<input type="password" name="password" value="<%=pass%>" /><br><br>
<input type="checkbox" name="rememberme" checked="<%=checked%>" />记住我
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="login" value="登录" /><br><br>
没有账号?&nbsp;&nbsp;<a href="signup.jsp">注册账号</a>
</form>
</div>
</body>
</html>

注册页面signup.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>账号注册</title>
</head>
<style>
 #a {
     width: 360px;
  	 padding: 8% 0 0;
     margin: auto;
}
</style>
<body>
<div id="a">
<h1>注册账号</h1>
<form action="signupsuccess.jsp" method="post">
账&nbsp;&nbsp;&nbsp;号:
<input type="text" name="id">
<br>
<br>
密&nbsp;&nbsp;&nbsp;码:
<input type="password" name="password">
<br>
<br>
<input type="submit" value="注册">
</form>
</div>
</body>
</html>

注册成功页面signupsuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册成功</title>
</head>
<style>
 #a {
     width: 360px;
  	 padding: 8% 0 0;
     margin: auto;
}
</style>
<body>
<div id="a">
<form action="check.jsp" method="post">
 <% request.setCharacterEncoding("UTF-8");
   String id=request.getParameter("id");
   session.setAttribute("id", id);
   String name=request.getParameter("name");
   session.setAttribute("name", name);

   String password=request.getParameter("password");
   session.setAttribute("password", password);
 %>
 
 恭喜您注册成功!<br>
 您的账号为:<%=id %><br>
 您的密码为:<%=password %><br>
 请妥善保管好您的密码!<br>
</form>
<a href="login.jsp" style="color:#FAAF46 font-size:10px">返回登录页面</a>
</div>
</body>
</html>

登录成功页面success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录成功</title>
<style>
 #a {
     width: 360px;
  	 padding: 8% 0 0;
     margin: auto;
}
</style>
</head>
<body>
<div id="a">
<h1 align="center">登陆成功</h1>
</div>
</body>
</html>

JavaBean

package com.wgh.bean;

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

login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head></head>
		<body>
			<form name="loginFrom" action="dologin.jsp?" method="POST">
			用户名:<input type="text" name="username" value="admin"/>
			密码:<input type="password" name="password" value="1111111"/>
			<input type="submit" value="登录"/>
			</form>
		</body>
</html>

dologin.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<jsp:useBean id="user" class="com.wgh.bean.User">
	<jsp:setProperty name="user" property="*"/>
</jsp:useBean>
<center>
	<b>用户名:</b><%=user.getUsername() %>
	<b>密码:</b><jsp:getProperty name="user" property="password"/>
</center>

标签:String,登录,学习,jsp,nbsp,JSP,password,页面
来源: https://blog.csdn.net/qq12559574610/article/details/121302167

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有