首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

刚才的有关问题重新编辑后,高分求答案

2011-12-25 
刚才的问题重新编辑后,高分求答案!我把我的登陆界面index.jsp和登陆后跳转的主界面success.jsp代码发在上

刚才的问题重新编辑后,高分求答案!
我把我的登陆界面index.jsp和登陆后跳转的主界面success.jsp代码发在上面
web形式的不是可以直接在地址烂里输入http://localhost:8084/success.jsp   而没有通过index.jsp.jsp嘛!现在要实现的功能是当非法输入后发现没经过index.jsp便弹到index.jsp要求登陆.不要用struts,直接session就行.麻烦把该加的代码加到相应位置!试了好几个都不对!
index.jsp
<%@page   contentType= "text/html "%>
<%@page   pageEncoding= "gb2312 "%>

<html>
        <head>
                <meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
        </head>
        <body>
                <script   language= "JavaScript ">

function   RgTest()   {
        if(document.login.userId.value== " ")   {
                window.alert( "请输入用户名! ");
                document.login.userId.focus();
                return   false;
        }
        if(document.login.passwd.value== " ")   {
                window.alert( "请输入密码! ");
                document.login.passwd.focus();
                return   false;
        }
        return   true;
}    
                </script>
                <center>
                        <h1> <font   color= "#0099CC "> 欢迎光临××运输管理系统 </font> </h1>
                        <form   method= "POST "   action= "check_login.jsp "   name= "login ">
                                <table     border= "1 "   bgcolor= "#0099CC "   align= "center ">
                                        <tr> <td>
                                                        <table>
                                                                <tr     bgcolor= "#009966 "   bordercolor= "#990066 ">
                                                                        <td   align= "right "> 姓名: </td>
                                                                        <td> <input   type= "text "   name= "userId "   value= " "   /> </td>


                                                                </tr>
                                                                <tr   bgcolor= "#009966 "   bordercolor= "#990066 ">
                                                                        <td   align= "right "> 密码: </td>
                                                                        <td> <input   type= "password "   name= "passwd "   value= " "   /> </td>
                                                                </tr>
                                                                <tr   bgcolor= "#009966 "   bordercolor= "#990066 ">
                                                                        <td   align= "right "> 登陆类型: </td>
                                                                        <td> <input   type= "radio "   name= "type "   value= "admin "   checked   /> 管理员 <input   type= "radio "   name= "type "   value= "user "   /> 普通用户 </td>
                                                                </tr>
                                                        </table>
                                        </td> </tr>
                                        <tr   bgcolor= "#009966 "   bordercolor= "#990066 "> <td   align= "center ">
                                                        <input   type= "submit "   value= "登陆 "   onClick= "return   RgTest() "   />


                                        </td> </tr>
                                </table>
                        </form>
                </center>
        </body>
</html>


[解决办法]
success.jsp

<%@page contentType= "text/html "%>
<%@page pageEncoding= "gb2312 "%>
<%@page import= "java.sql.*,java.io.* "%>
<jsp:useBean id= "dataConnection " scope= "page " class= "org.me.dataConnection "> </jsp:useBean>
<!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=gb2312 ">
</head>
<body>
<script language= "JavaScript ">

function RgTest() {
var numtype=/^[0-9]{1,}$/;
var idcard=/^[0-9]{18}$/;
if(document.user.userId.value== " ") {
window.alert( "请输入用户ID! ");
document.user.userId.focus();
return false;
}
if(document.user.userName.value== " ") {
window.alert( "请输入用户姓名! ");
document.user.userName.focus();
return false;
}
if(document.user.passwd.value== " ") {
window.alert( "请输入密码! ");
document.user.passwd.focus();
return false;
}
if(document.user.repasswd.value== " ") {
window.alert( "请输入确认密码! ");
document.user.repasswd.focus();
return false;
}
if(document.user.age.value== " "||document.user.age.value <=20||document.user.age.value> =60) {
window.alert( "请输入用户年龄,且必须在20到60之间! ");
document.user.age.focus();
return false;
}
if(!(numtype.exec(document.user.age.value))) {
window.alert( "用户年龄必须是数字! ");
document.user.age.focus();
return false;
}
if(document.user.idCard.value== " ") {
window.alert( "请输入身份证! ");
document.user.idCard.focus();
return false;
}
if(!(idcard.exec(document.user.idCard.value))) {
window.alert( "身份证必须18位! ");
document.user.idCard.focus();
return false;
}
if(document.user.salay.value== " ") {
window.alert( "请输入用户工资! ");
document.user.salay.focus();
return false;
}
if(!(numtype.exec(document.user.salay.value))) {
window.alert( "用户工资必须是数字! ");
document.user.salay.focus();
return false;
}
if(document.user.passwd.value!=document.user.repasswd.value)
{
window.alert( "密码输入有误,请重新输入! ");
document.user.passwd.value= " ";
document.user.repasswd.value= " ";
document.user.passwd.focus();
return false;
}
return true;


}
</script>

<%@ include file= "adminlink.jsp "%>
<center>
<h1> 用户注册 </h1>
<form action= "addUser_do.jsp " method= "post " name= "user ">
<table border= "1 " bgcolor= "#0099CC ">
<tr> <td>
<table border= "1 ">
<tr> <td width= "100px " align= "right "> 用户ID: </td> <td align= "left "> <input type= "text " name= "userId "> </td> </tr>
<tr> <td width= "100px " align= "right "> 用户名: </td> <td align= "left "> <input type= "text " name= "userName "> </td> </tr>
<tr> <td width= "100px " align= "right "> 密码: </td> <td align= "left "> <input type= "password " name= "passwd "> </td> </tr>
<tr> <td width= "100px " align= "right "> 密码确认: </td> <td align= "left "> <input type= "password " name= "repasswd "> </td> </tr>
<tr> <td width= "100px " align= "right "> 负责车辆: </td> <td align= "left "> <select name= "carId ">

<%
Connection conn=dataConnection.getConnection();
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery( "select * from car where free=true ");
while(rs.next()){
out.println( " <option > "+rs.getString( "carId ")+ " </option> ");
}

rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
System.out.println( "Error: "+e.toString());
}
%>
<option> null </option>
</select> </td> </tr>
<tr> <td width= "100px " align= "right "> 年龄: </td> <td align= "left "> <input type= "text " name= "age "> </td> </tr>
<tr> <td width= "100px " align= "right "> 性别: </td> <td align= "left "> <select name= "sex ">
<option value= "男 " selected> 男 </option>
<option value= "女 "> 女 </option>
</select> </td> </tr>


<tr> <td width= "100px " align= "right "> 身份证: </td> <td align= "left "> <input type= "text " name= "idCard "> </td> </tr>
<tr> <td width= "100px " align= "right "> 工资: </td> <td align= "left "> <input type= "text " name= "salay "> </td> </tr>
<tr> <td width= "100px " align= "right "> 其他: </td> <td align= "left "> <textarea name= "profile " rows= "4 " cols= "20 ">
</textarea> </td> </tr>
</table>
</td> </tr>
<tr> <td>
<input type=submit value=提交 onClick= "return RgTest() ">
</td> </tr>
</table>
</form>
</center>
</body>
</html>

[解决办法]
还有个checklogin.jsp
<%@page contentType= "text/html "%>
<%@page pageEncoding= "gb2312 "%>
<%@page import= "java.sql.* "%>
<%@ include file= "tran.jsp " %>
<jsp:useBean id= "dataConnection " scope= "page " class= "org.me.dataConnection "> </jsp:useBean>
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">

</head>
<body>
<%
String userId=tranc(request.getParameter( "userId "));
String passwd=request.getParameter( "passwd ");
String type=request.getParameter( "type ");

Connection conn=dataConnection.getConnection();
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery( "select passwd from user where userId= ' "+userId+ " 'and type= ' "+type+ " ' ");

if(!rs.next()){
response.sendRedirect( "index.jsp ");
} else{
if(passwd.equals(rs.getString( "passwd "))){
if(type.equals( "admin "))
response.sendRedirect( "success.jsp ");
else {
session.setAttribute( "userId ",userId);
response.sendRedirect( "searchOrder.jsp ");
}
}
else{
response.sendRedirect( "index.jsp ");
}
}
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
System.out.println( "Error: "+e.toString());
}

%>
</body>
</html>

[解决办法]
直接求正确程序!
[解决办法]
帮顶下
------解决方案--------------------


都知道用SESSION了,还有什么可问的呢,读写SESSION还不会吗,搜一下也有了。
有问有答是正常的,但象这样“布置作业”一样的方式就不太招人喜欢了。
[解决办法]
楼上的我知道啊,但我弄了很长时间都没搞对,我急着赶出来,所以麻烦帮下忙!不好意思!
[解决办法]
LZ 借你地方用下


theforever(碧海情天) ( ) 信誉:100 Blog 加为好友 2007-5-8 11:40:07 得分: 20
利用数据库存储功能数据,然后动态加载功能模块。
比如一个功能菜单,每次从数据库提取菜单项目和对应的功能页面,新增的功能模块就可以自然地随时呈现。

===================================
能不能告诉下那里有这方面的资料




能不能发个Demo看看
[解决办法]
在其它页的顶部检验一下SESSION是否为空即可,如下:
<%
String sess = session.getAttribute( "userId ") ;
if (sess == null || sess.equals( " ") || sess.equals( "null "))
response.sendRedirect( "index.jsp ");

%>
[解决办法]
楼上的,好象还欠缺点
[解决办法]
lz的问题好象发了两次。。。

利用过滤器进行session判断,代码如下:

************* login.jsp ****************

<%@ page language= "java " import= "java.util.* " pageEncoding= "GBK "%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> My JSP 'login.jsp ' starting page </title>
</head>

<body>
<form action= "checkservlet " method= "post ">
username:
<input type= "text " name= "uname " />
<br />
password:
<input type= "text " name= "pwd " />
<br />
<input type= "submit " value= "提交 " />
</form>
</body>
</html>
************* index.jsp ****************

<%@ page language= "java " import= "java.util.* " pageEncoding= "ISO-8859-1 "%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> My JSP 'index.jsp ' starting page </title>
</head>
<body>
Login Scuccess!
</body>
</html>
************* CheckServlet.java ****************

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String uname = request.getParameter( "uname ");
String pwd = request.getParameter( "pwd ");

if (uname.equals( "test ") && pwd.equals( "test ")) {
//验证成功写入session
request.getSession().setAttribute( "check ", "success ");
response.sendRedirect( "index.jsp ");
} else {
response.sendRedirect( "login.jsp ");
}
}
}

************* CheckLoginFilter.java ****************

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;


import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckLoginFilter implements Filter {

public void destroy() {
// TODO 自动生成方法存根
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;

//判断session是否存在
if (null == request.getSession().getAttribute( "check ")) {
response.sendRedirect( "login.jsp ");
} else {
arg2.doFilter(arg0, arg1);
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO 自动生成方法存根
}
}

************* web.xml ****************

<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app version= "2.4 " xmlns= "http://java.sun.com/xml/ns/j2ee "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<servlet>
<servlet-name> CheckServlet </servlet-name>
<servlet-class> CheckServlet </servlet-class>
</servlet>

<servlet-mapping>
<servlet-name> CheckServlet </servlet-name>
<url-pattern> /checkservlet </url-pattern>
</servlet-mapping>

<filter>
<filter-name> CheckLoginFilter </filter-name>
<filter-class> CheckLoginFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> CheckLoginFilter </filter-name>
<url-pattern> /index.jsp </url-pattern>
</filter-mapping>

</web-app>

热点排行