页面:login.jsp,登录后的index.jsp,test.jsp, 以及checklogin.jsp;我想在访问index.jsp,test.jsp之前先做登录校验,登录后才能index.jsp,test.jsp,想使用@include指令引用校验页该如何实现;代码:
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><%
request.setCharacterEncoding("GBK");
String name = request.getParameter("user");
String pwd = request.getParameter("pwd");
System.out.println(name+" "+pwd);
if(name!=null&&pwd!=null){
if(name.equals("sa")&&pwd.equals("123")){
session.setAttribute("Login","OK");
response.sendRedirect("index.jsp");
}
else
{
out.println("登录失败,请重新输入。");
response.sendRedirect("error.jsp");
}
}
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP </title>
</head>
<body>
<form id="form1" name="form" method="post" action="./testSession/login.jsp">
姓名:<input type="text" name="user" />
<br>
密码: <input type="password" name="pwd" />
<br>
<input type="submit" name="sub" value="登录" />
</form> </body>
</html>
checklogin.jsp
<%
request.setCharacterEncoding("GBK");
String login=null;
try{
login = session.getAttribute("Login").toString();
if (login.equals("OK")) {
JOptionPane.showMessageDialog(null,"用户登录,欢迎进入主页。");
session.invalidate();
response.sendRedirect("index.jsp");
}
}catch(Exception e){
if (login==null) {
JOptionPane.showMessageDialog(null,"用户没有登录,请登录");
response.sendRedirect("login.jsp");
}
}
%>index.jsp:
<html>
<body>
<%
if(session.getAttribute("Login")==null){
%>
<%@include file="checklogin.jsp" %>
<br>
<%} %>
welcome to index.
</body>
</html>test.jsp:
<html>
<body>
<%
if(session.getAttribute("Login")==null){
%>
<%@include file="checklogin.jsp" %>
<br>
<%} %>
welcome to index.
</body>
</html>我直接输入http://localhost:8080/test.jsp不跳回login.jsp,不知如何修改,请教?
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><%
request.setCharacterEncoding("GBK");
String name = request.getParameter("user");
String pwd = request.getParameter("pwd");
System.out.println(name+" "+pwd);
if(name!=null&&pwd!=null){
if(name.equals("sa")&&pwd.equals("123")){
session.setAttribute("Login","OK");
response.sendRedirect("index.jsp");
}
else
{
out.println("登录失败,请重新输入。");
response.sendRedirect("error.jsp");
}
}
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP </title>
</head>
<body>
<form id="form1" name="form" method="post" action="./testSession/login.jsp">
姓名:<input type="text" name="user" />
<br>
密码: <input type="password" name="pwd" />
<br>
<input type="submit" name="sub" value="登录" />
</form> </body>
</html>
checklogin.jsp
<%
request.setCharacterEncoding("GBK");
String login=null;
try{
login = session.getAttribute("Login").toString();
if (login.equals("OK")) {
JOptionPane.showMessageDialog(null,"用户登录,欢迎进入主页。");
session.invalidate();
response.sendRedirect("index.jsp");
}
}catch(Exception e){
if (login==null) {
JOptionPane.showMessageDialog(null,"用户没有登录,请登录");
response.sendRedirect("login.jsp");
}
}
%>index.jsp:
<html>
<body>
<%
if(session.getAttribute("Login")==null){
%>
<%@include file="checklogin.jsp" %>
<br>
<%} %>
welcome to index.
</body>
</html>test.jsp:
<html>
<body>
<%
if(session.getAttribute("Login")==null){
%>
<%@include file="checklogin.jsp" %>
<br>
<%} %>
welcome to index.
</body>
</html>我直接输入http://localhost:8080/test.jsp不跳回login.jsp,不知如何修改,请教?
解决方案 »
- 如何去自动抓取其他网站上的新闻信息
- Java 银行家算法
- 一个猜数字的简单小程序
- spring struts oc4j log4j
- 关于CobrowseServlet
- weblogic中classpath和web appliaction中lib下,有版本不同的两个jar包,会加载哪个?
- 那位遇到了WEBLOGIC经常出问题,需要重起就好了的问题?如何解决?
- 各位大侠 帮忙提供WEBLOGIC6.1 sp1的license.bea
- Springset注入接口报空指针异常
- 关于 hibernate 双向一对多的数据保存
- 刚学java不久,请教如何让一个Action内的多个不同DAO层事务手动提交?谢谢~~~
- servlet:No buffer space available
类似 public class CharsetEncodingFilter implements Filter{...} 然后在web.xml里面配置一下
另外security机制实现能否给个案例啊。