Session登录的问题
Login.jsp
-------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎页面</title>
<style type="text/css">
body {
font-family: 黑体;
font-size: 20px;
font-weight: bold;
}
</style>
<script type="text/javascript">
var xmlhttp;
function checkeds1() {
var userInput = document.getElementById("userInput").value;
if(userInput=="")
{
document.getElementById("show").innerHTML="<font color='red'>您没有选择登录功能</font>";
return ;
}
if(userInput!=1 && userInput!=2 )
{
document.getElementById("show").innerHTML="<font color='red'>您输入的不正确</font>";
return ;
}
document.getElementById("show").innerHTML="";
}
function checkeds2() {
var managerId=document.getElementById("managerId").value;
if(managerId=="")
{
document.getElementById("managers").innerHTML="<font color='red'>管理员账号不能为空</font>";
return ;
}
document.getElementById("managers").innerHTML="";
}
function checkeds3()
{
var managerPWD=document.getElementById("managerPWD").value;
if(managerPWD=="")
{
document.getElementById("managerssPWD").innerHTML="<font color='red'>管理员密码不能为空</font>";
return ;
}
document.getElementById("managerssPWD").innerHTML="";
}
function createHttpRequest() //创建XMLHttpRequest对象
{
if(window.XMLHttpRequest)
{
xmlhttp =new XMLHttpRequest();
}else if(window.ActiveXObject)
{
xmlhttp =new ActiveXObject("Microfost.XMLHTTP");
}
}
function send_httpRequest()
{
var managerId=document.getElementById("managerId").value;
var managerPWD=document.getElementById("managerPWD").value;
createHttpRequest();
xmlhttp.open("post","index?managerId="+managerId+"&&managerPWD="+managerPWD,true);
xmlhttp.onreadystatechange=Callback;
xmlhttp.send(null);
}
function Callback()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
var text = xmlhttp.responseText;
alert(text);
if(text=="true")
{
document.getElementById("form1").submit();
}
else
{
checkeds3();
return ;
}
}
}
}
</script>
</head>
<body style="background-color: #999">
<div style="padding-top: 100px;" align="center">
<form action="showManager.jsp" id="form1" method="post">
<table width="1024" height="223">
<tr>
<td colspan="2" align="center">
欢迎你进入商品管理系统
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2" align="center">
1.商品购物系统
</td>
</tr>
<tr>
<td colspan="2" align="center">
2.商品管理系统
</td>
</tr>
<tr>
<td width="465" height="21" align="center">
如果你是客户请选择<1>
</td>
<td width="547" align="center">
如果你是管理员请选择<2>
</td>
</tr>
<tr>
<td width="465" align="right">
请输入:
<input type="text" name="userInput" id="userInput"
onkeypress="myKeyDown();" onblur="checkeds1()" />
</td>
<td width="547" id="show" align="left">
</td>
</tr>
<tr id="divid" >
<td width="465" align="right">
请输入管理员帐号:
<input type="text" name="manager" id="managerId"
onblur="checkeds2()" />
</td>
<td width="547" id="managers" align="left">
</td>
</tr>
<tr id="divid1" >
<td width="465" align="right">
请输入管理员密码:
<input type="text" name="managersPWD" id="managerPWD"
onblur="checkeds3()" />
</td>
<td width="547" id="managerssPWD" align="left">
<!-- 注意在遨游中document.getElementById既可以按照id获取,也可以按照name获取 -->
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="button" value="登录" onclick="send_httpRequest()"
style="background-color: #666666; border: 1px 1px 1px 1px; font-weight: bold;"
width="100px;" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
index servlet文件
--------------------
package cn.jbit.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class index extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("true");
request.getSession().setAttribute("win", "win");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
------------------------
showManager.jsp代码
---------------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>主页面</title>
<style type="text/css">
body {
font-family: 黑体;
font-size: 25px;
font-weight: bold;
}
a {
text-decoration: none;
}
a:ACTIVE {
color: blue;
}
a:LANG {
color: blue;
}
a:VISITED {
color: blue;
}
a:HOVER {
color: red;
}
</style>
</head>
<body style="background-color: #999">
<c:if test="${ empty sessionScope.win }">
<script>
alert("您没有登录,请重新登录");
window.location.href = "Login.jsp";
</script>
</c:if>
<div style="padding-top: 100px;" align="center">
<table width="995" height="299" style="text-align: center;"
cellspacing="20px;">
<tr>
<td>
<span style="font-size: 30px;">商品管理系统>商品主菜单</span>
</td>
</tr>
<tr>
<td>
<hr size="+5" />
</td>
</tr>
<tr>
<td>
<a href="">1.添加商品信息</a>
</td>
</tr>
<tr>
<td>
<a href="">2.修改商品信息</a>
</td>
</tr>
<tr>
<td>
<a href="">3.删除商品信息</a>
</td>
</tr>
<tr>
<td>
<a href="">4.查询商品信息</a>
</td>
</tr>
<tr>
<td>
<a href="">5.添加会员信息</a>
</td>
</tr>
<tr>
<td>
<a href="">6.查看所有会员信息</a>
</td>
</tr>
<tr>
<td>
<a href="">0.退出</a>
</td>
</tr>
</table>
<hr size="+5" width="995" />
</div>
</body>
</html>
------------------------------
问题是 我达到不能直接登录 showManager.jsp页面 必须通过登录页面登录之后 才能直接登录showManager.jsp页面 但是我通过
<c:if test="${ empty sessionScope.win }">
<script>
alert("您没有登录,请重新登录");
window.location.href = "Login.jsp";
</script>这段代码测试了一下 为什么 我从登录窗口直接登录到showManager.jsp之后 我在直接登录showManager.jsp登录 还告诉我 没有登录 请高手给运行以下代码 仔细看看
[解决办法]
你上面代码是完整的吗 上面要不要连接数据库啊 不用的话发过来看看
[解决办法]
你登录后把sessionScope.win这个win值打印在前台看看,看是你没取到,还是你写的方法有问题!!