初识javaweb jsp登录案例

此博客只作为学习备忘 大神请绕过

login.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>
head>
<body>

<form action="dologin.jsp" method="post">

UserName:<input type="text" name="UserName">
UserPwd:<input type="password" name="UserPwd">
<button type="submit">登陆button>

form>

body>
html>

登录处理jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"%>

<% 
//设置响应编码为utf-8
request.setCharacterEncoding("utf-8");

String UserName=request.getParameter("UserName"),UserPwd=request.getParameter("UserPwd");

if("admin".equals(UserName)&&"admin".equals(UserPwd)){
     //存储session 必须在转发之前设置 
        session.setAttribute("UserName", UserName);
    //表示登陆成功  将请求转发
    request.getRequestDispatcher("login_success.jsp").forward(request, response);



    //设置过期时间 10秒过期
    //session.setMaxInactiveInterval(10);


}else 
    response.sendRedirect("login_error.jsp");

%>

登录成功后转发页面 显示登录成功点的用户名

<%@ page language="java" contentType="text/html; charset=utf-8"
    %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>
head>
<body>
<h1>   

恭喜 <%= session.getAttribute("UserName")  %>,登陆成功

h1>
body>
html>

你可能感兴趣的:(java)