login.jsp代码:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String action=request.getParameter("action");
if(action != null && action.equals("login")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if( username == null || !username.equals("admin")) {
out.println("用户名不正确");
}
else if(password == null || !password.equals("admin")) {
out.println("密码不正确");
}else {
session.setAttribute("admin", "admin");
response.sendRedirect("index.jsp");
}
}
%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form name="form" action="login.jsp" method="post">
<input type="hidden" name="action" value="login"/>
<table border=1 algin="center">
<tr align=center>
<td colspan="2" align="center">用户注册</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" size=18/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" size=12/></td>
</tr>
<tr>
<td></td>
<td>
<input type=submit value="登陆"/>
<input type=reset value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String action=request.getParameter("action");
if(action != null && action.equals("login")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if( username == null || !username.equals("admin")) {
out.println("用户名不正确");
}
else if(password == null || !password.equals("admin")) {
out.println("密码不正确");
}else {
session.setAttribute("admin", "admin");
response.sendRedirect("index.jsp");
}
}
%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form name="form" action="login.jsp" method="post">
<input type="hidden" name="action" value="login"/>
<table border=1 algin="center">
<tr align=center>
<td colspan="2" align="center">用户注册</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" size=18/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" size=12/></td>
</tr>
<tr>
<td></td>
<td>
<input type=submit value="登陆"/>
<input type=reset value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
要转到的页面index.jsp<%@ page language="java" contentType="text/html; charset=gb2312" %><%@ include file="_sessionCheck.jsp" %><html>
<script language="javascript">
state = 0 ;
menuState = 0;
mainState = 0;
</script><head>
<title>金尚管理平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><frameset rows="29,*" frameborder="0" border="0" framespacing="0" cols="*">
<frame name="top" scrolling="NO" noresize src="top.html">
<frameset cols="20%,*" frameborder="0" border="0" framespacing="0" rows="*" scrolling="NO" name="mleft">
<frame src="menu.html" frameborder=NO border="0" scrolling="NO" >
<frameset rows="20,100%,*" name="content" frameborder="1" framespacing="1" cols="*">
<frame src="title.html" frameborder=0 noresize scrolling="NO" name="mtitle">
<frame src="" frameborder=0 name="main" marginwidth="0" marginheight="0" scrolling="YES">
<frame src="" frameborder=0 name="detail">
</frameset>
</frameset>
</frameset>
<noframes>
</noframes>
</html>
<%
String admin = (String)session.getAttribute("admin");
if(admin == null || !admin.equals("true")) {
response.sendRedirect("login.jsp");
}
%>
必须要先登录,才能进入页面!!
这个函数的参数我传错了,应该是这样
session.setAttribute("admin", "true");
我自己拿分了
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String action=request.getParameter("action");
if(action != null && action.equals("login")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if( username == null || !username.equals("admin")) {
out.println("用户名不正确");
}
else if(password == null || !password.equals("admin")) {
out.println("密码不正确");
}else {
session.setAttribute("admin", "admin");
response.sendRedirect("index.jsp");
}
}
%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form name="form" action="login.jsp" method="post">
<input type="hidden" name="action" value="login"/>
<table border=1 algin="center">
<tr align=center>
<td colspan="2" align="center">用户注册</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" size=18/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" size=12/></td>
</tr>
<tr>
<td></td>
<td>
<input type=submit value="登陆"/>
<input type=reset value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>