%TOMCAT_HOME%\webapps\ @1
mysample/login.html @2
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
我照样作了:
http://localhost:8080/mysample/login.html
不好使呀?
另:@1与@2怎么不一样呀?
8 个解决方案
#1
补充:@1-'\'与@2-'/'怎么不一样呀?
#2
jsp放在root下面,例如:C:\Tomcat 5.0\webapps\ROOT
servlet放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes里
javabean放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\你的包 里面
注意,servlet要注册
C:\Tomcat 5.0\webapps\ROOT\WEB-INF里有个web.xml,把你的servlet注册一下
例如:
<servlet>
<servlet-name>FlyServlet</servlet-name>
<servlet-class>FlyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlyServlet</servlet-name>
<url-pattern>/servlet/FlyServlet</url-pattern>
</servlet-mapping>
servlet放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes里
javabean放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\你的包 里面
注意,servlet要注册
C:\Tomcat 5.0\webapps\ROOT\WEB-INF里有个web.xml,把你的servlet注册一下
例如:
<servlet>
<servlet-name>FlyServlet</servlet-name>
<servlet-class>FlyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlyServlet</servlet-name>
<url-pattern>/servlet/FlyServlet</url-pattern>
</servlet-mapping>
#3
因为root下有别的项目文件,为不使混乱,我想知道得更细一些。
请拿%TOMCAT_HOME%\webapps\ @1
mysample/login.html @2
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
作例子,教小弟一下。万分感谢!!!
请拿%TOMCAT_HOME%\webapps\ @1
mysample/login.html @2
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
作例子,教小弟一下。万分感谢!!!
#4
'\'与'/'的作用是一样的,只是我们在编程中,用String 变量时要注意,'\'会起转义符的作用.
添要一个子目录的方法:
在webapps中新建一个目录,其结构如下:
\webapps\你的目录
\WEB-INF
\classes
\lib
在\conf目录中找到server.xml文件,打开
在<Context path="" docBase="ROOT" debug="0"/>后面加上:
<Context path="\gogo" docBase="你的目录" debug="0"/>
这样,当你输入localhost\gogo\123.jsp时就会访问"你的目录"下面的123.jsp
添要一个子目录的方法:
在webapps中新建一个目录,其结构如下:
\webapps\你的目录
\WEB-INF
\classes
\lib
在\conf目录中找到server.xml文件,打开
在<Context path="" docBase="ROOT" debug="0"/>后面加上:
<Context path="\gogo" docBase="你的目录" debug="0"/>
这样,当你输入localhost\gogo\123.jsp时就会访问"你的目录"下面的123.jsp
#5
使这样吗?:
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
执行语句:localhost:8080\go\login.html
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
执行语句:localhost:8080\go\login.html
#6
呵呵,老兄你是青鸟哪个培训中心的啊??
#7
配置server.xml如下:
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
运行tomcat:
1.http://localhost:8090/go/login.html 不好使!
2.http://localhost:8090/ 连这个都不好使了!
首先,<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>应改为:
<Context path="/go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
其次,你得把注释符号,就是<!--和-->给去掉
再试试看吧
首先
<!--
<Context path="" docBase="ROOT" debug="0"/> -->
系统本身就如此,所以<!--和-->去掉不像正确的样子。
另外:http://localhost:8090/go/login.html出错:
HTTP Status 404 - /go/login.html
--------------------------------------------------------------------------------
type Status report
message /go/login.html
description The requested resource (/go/login.html) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/4.1.18
晴大哥们不吝赐教.
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
运行tomcat:
1.http://localhost:8090/go/login.html 不好使!
2.http://localhost:8090/ 连这个都不好使了!
首先,<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>应改为:
<Context path="/go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
其次,你得把注释符号,就是<!--和-->给去掉
再试试看吧
首先
<!--
<Context path="" docBase="ROOT" debug="0"/> -->
系统本身就如此,所以<!--和-->去掉不像正确的样子。
另外:http://localhost:8090/go/login.html出错:
HTTP Status 404 - /go/login.html
--------------------------------------------------------------------------------
type Status report
message /go/login.html
description The requested resource (/go/login.html) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/4.1.18
晴大哥们不吝赐教.
#8
如何配置路径,请大哥们不吝赐教!!!
环境:Tomcat
文件目录如下:
%TOMCAT_HOME%\webapps\
mysample/login.html
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
//////////////////////login.html ////////////////////////
<HTML>
<HEAD>
</HEAD>
<BODY>
<form name="form1" method="post" action="/servlet/forky">
用户名:
<input type="text" name="username" size="15"><br><br>
密码:
<input type="password" name="password" size="15"><br><br>
<input type="submit" name="Submit" value="确定">
</form>
</BODY>
</HTML>
////////////////////////validate.jsp:////////////////////////
<%!
boolean isnotlogin=false;
%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username==null||password==null)
{
response.sendRedirect("error.jsp?errmsg=页面值传递错误,或者非法进入该页面(在validate.jsp中)");return;
}
%>
<jsp:useBean id="validatebean" scope="page" class="mypackage.validate"/>
<jsp:setProperty name="validatebean" property="username" param="username"/>
<jsp:setProperty name="validatebean" property="pwd" param="password"/>
</jsp:useBean>
<%
isnotlogin =validatebean.uservalidate();
if(!isnotlogin)
{
response.sendRedirect("error.jsp?errmsg=用户名或者口令错误!(在validate.jsp中)");return;
}
else
{
session.setAttribute("username",username);
%>
<jsp:forward page="servlet/bean">
<jsp:param name="username" value="<%=username%>"/>
</jsp:forward>
<%
}
%>
////////////////////////jb_aptech.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在jb_aptech.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在jb_aptech.jsp中)");return;
}
out.print("<br>);
out.print("欢迎");
out.print("<br>");
out.print("同学们");
out.print("<br>");
%>
////////////////////////teacher.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在teacher.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在teacher.jsp中)");return;
}
out.print("哈哈!");
out.print("<br>");
out.print("老师们");
out.print("<br>");
out.print("同,成功");
out.print("<br>");
out.print("努力");
////////////////////////error.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%String str=request.getParameter("errmsg");
String errmsg=new String(str.getBytes("ISO-8859-1"),"GBK");
%>
<html>
<head>
</head>
<body>
对不起,您的操作有误。请参考下列提示:<p>
<b><%=errmsg%></b>
</body>
</html>
////////////////////////web.xml:////////////////////////
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Micrcosystems,Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>forky</servlet-name>
<servlet-class>forky</servlet-class>
</servlet>
</web-app>
////////////////////////validate.java:////////////////////////
package mypackage;
public class validate
{
final String user1="jb";
final String password1="jb";
final String user2="aptech";
final String password2="aptech";
final String user3="teacher";
final String password3="student";
private String username="";
private String pwd="";
public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return this.username;
}
public void setPwd(String password)
{
pwd=password;//this.
}
public boolean uservalidate()
{
boolean temp =false;
if(username.equals(user1)&&pwd.equals(password1))
{
temp=true;
}
else if(username.equals(user2)&&pwd.equals(password2))
{
temp=true;
}
else if(username.equals(user3)&&pwd.equals(password3))
{
temp=true;
}
else
{
temp=false;
}
return temp;
}
}
////////////////////////forky.java:////////////////////////
package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class forky extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
HttpSession session=request.getSession(false);
if(session==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在servlet:forky中)");return;
}
String username1=(String)session.getAttribute("username");
String username2=request.getParameter("usrename");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在servlet:forky中)");return;
}
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
request.setAttribute("username",username2);
if(username2.equals("teacher"))
{
getServletConfig().getServletContext().getRequestDispatcher("/tercher.jsp").forward(request,response);
}
else
{
getServletConfig().getServletContext().getRequestDispatcher("/jb_aptech.jsp").forward(request,response);
}
}
}
环境:Tomcat
文件目录如下:
%TOMCAT_HOME%\webapps\
mysample/login.html
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
//////////////////////login.html ////////////////////////
<HTML>
<HEAD>
</HEAD>
<BODY>
<form name="form1" method="post" action="/servlet/forky">
用户名:
<input type="text" name="username" size="15"><br><br>
密码:
<input type="password" name="password" size="15"><br><br>
<input type="submit" name="Submit" value="确定">
</form>
</BODY>
</HTML>
////////////////////////validate.jsp:////////////////////////
<%!
boolean isnotlogin=false;
%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username==null||password==null)
{
response.sendRedirect("error.jsp?errmsg=页面值传递错误,或者非法进入该页面(在validate.jsp中)");return;
}
%>
<jsp:useBean id="validatebean" scope="page" class="mypackage.validate"/>
<jsp:setProperty name="validatebean" property="username" param="username"/>
<jsp:setProperty name="validatebean" property="pwd" param="password"/>
</jsp:useBean>
<%
isnotlogin =validatebean.uservalidate();
if(!isnotlogin)
{
response.sendRedirect("error.jsp?errmsg=用户名或者口令错误!(在validate.jsp中)");return;
}
else
{
session.setAttribute("username",username);
%>
<jsp:forward page="servlet/bean">
<jsp:param name="username" value="<%=username%>"/>
</jsp:forward>
<%
}
%>
////////////////////////jb_aptech.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在jb_aptech.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在jb_aptech.jsp中)");return;
}
out.print("<br>);
out.print("欢迎");
out.print("<br>");
out.print("同学们");
out.print("<br>");
%>
////////////////////////teacher.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在teacher.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在teacher.jsp中)");return;
}
out.print("哈哈!");
out.print("<br>");
out.print("老师们");
out.print("<br>");
out.print("同,成功");
out.print("<br>");
out.print("努力");
////////////////////////error.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%String str=request.getParameter("errmsg");
String errmsg=new String(str.getBytes("ISO-8859-1"),"GBK");
%>
<html>
<head>
</head>
<body>
对不起,您的操作有误。请参考下列提示:<p>
<b><%=errmsg%></b>
</body>
</html>
////////////////////////web.xml:////////////////////////
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Micrcosystems,Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>forky</servlet-name>
<servlet-class>forky</servlet-class>
</servlet>
</web-app>
////////////////////////validate.java:////////////////////////
package mypackage;
public class validate
{
final String user1="jb";
final String password1="jb";
final String user2="aptech";
final String password2="aptech";
final String user3="teacher";
final String password3="student";
private String username="";
private String pwd="";
public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return this.username;
}
public void setPwd(String password)
{
pwd=password;//this.
}
public boolean uservalidate()
{
boolean temp =false;
if(username.equals(user1)&&pwd.equals(password1))
{
temp=true;
}
else if(username.equals(user2)&&pwd.equals(password2))
{
temp=true;
}
else if(username.equals(user3)&&pwd.equals(password3))
{
temp=true;
}
else
{
temp=false;
}
return temp;
}
}
////////////////////////forky.java:////////////////////////
package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class forky extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
HttpSession session=request.getSession(false);
if(session==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在servlet:forky中)");return;
}
String username1=(String)session.getAttribute("username");
String username2=request.getParameter("usrename");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在servlet:forky中)");return;
}
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
request.setAttribute("username",username2);
if(username2.equals("teacher"))
{
getServletConfig().getServletContext().getRequestDispatcher("/tercher.jsp").forward(request,response);
}
else
{
getServletConfig().getServletContext().getRequestDispatcher("/jb_aptech.jsp").forward(request,response);
}
}
}
#1
补充:@1-'\'与@2-'/'怎么不一样呀?
#2
jsp放在root下面,例如:C:\Tomcat 5.0\webapps\ROOT
servlet放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes里
javabean放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\你的包 里面
注意,servlet要注册
C:\Tomcat 5.0\webapps\ROOT\WEB-INF里有个web.xml,把你的servlet注册一下
例如:
<servlet>
<servlet-name>FlyServlet</servlet-name>
<servlet-class>FlyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlyServlet</servlet-name>
<url-pattern>/servlet/FlyServlet</url-pattern>
</servlet-mapping>
servlet放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes里
javabean放在:C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\你的包 里面
注意,servlet要注册
C:\Tomcat 5.0\webapps\ROOT\WEB-INF里有个web.xml,把你的servlet注册一下
例如:
<servlet>
<servlet-name>FlyServlet</servlet-name>
<servlet-class>FlyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FlyServlet</servlet-name>
<url-pattern>/servlet/FlyServlet</url-pattern>
</servlet-mapping>
#3
因为root下有别的项目文件,为不使混乱,我想知道得更细一些。
请拿%TOMCAT_HOME%\webapps\ @1
mysample/login.html @2
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
作例子,教小弟一下。万分感谢!!!
请拿%TOMCAT_HOME%\webapps\ @1
mysample/login.html @2
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
作例子,教小弟一下。万分感谢!!!
#4
'\'与'/'的作用是一样的,只是我们在编程中,用String 变量时要注意,'\'会起转义符的作用.
添要一个子目录的方法:
在webapps中新建一个目录,其结构如下:
\webapps\你的目录
\WEB-INF
\classes
\lib
在\conf目录中找到server.xml文件,打开
在<Context path="" docBase="ROOT" debug="0"/>后面加上:
<Context path="\gogo" docBase="你的目录" debug="0"/>
这样,当你输入localhost\gogo\123.jsp时就会访问"你的目录"下面的123.jsp
添要一个子目录的方法:
在webapps中新建一个目录,其结构如下:
\webapps\你的目录
\WEB-INF
\classes
\lib
在\conf目录中找到server.xml文件,打开
在<Context path="" docBase="ROOT" debug="0"/>后面加上:
<Context path="\gogo" docBase="你的目录" debug="0"/>
这样,当你输入localhost\gogo\123.jsp时就会访问"你的目录"下面的123.jsp
#5
使这样吗?:
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
执行语句:localhost:8080\go\login.html
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
执行语句:localhost:8080\go\login.html
#6
呵呵,老兄你是青鸟哪个培训中心的啊??
#7
配置server.xml如下:
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
运行tomcat:
1.http://localhost:8090/go/login.html 不好使!
2.http://localhost:8090/ 连这个都不好使了!
首先,<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>应改为:
<Context path="/go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
其次,你得把注释符号,就是<!--和-->给去掉
再试试看吧
首先
<!--
<Context path="" docBase="ROOT" debug="0"/> -->
系统本身就如此,所以<!--和-->去掉不像正确的样子。
另外:http://localhost:8090/go/login.html出错:
HTTP Status 404 - /go/login.html
--------------------------------------------------------------------------------
type Status report
message /go/login.html
description The requested resource (/go/login.html) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/4.1.18
晴大哥们不吝赐教.
<!--
<Context path="" docBase="ROOT" debug="0"/>
<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
-->
运行tomcat:
1.http://localhost:8090/go/login.html 不好使!
2.http://localhost:8090/ 连这个都不好使了!
首先,<Context path="\go" docBase="C:\tomcat\webapps\mysample" debug="0"/>应改为:
<Context path="/go" docBase="C:\tomcat\webapps\mysample" debug="0"/>
其次,你得把注释符号,就是<!--和-->给去掉
再试试看吧
首先
<!--
<Context path="" docBase="ROOT" debug="0"/> -->
系统本身就如此,所以<!--和-->去掉不像正确的样子。
另外:http://localhost:8090/go/login.html出错:
HTTP Status 404 - /go/login.html
--------------------------------------------------------------------------------
type Status report
message /go/login.html
description The requested resource (/go/login.html) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/4.1.18
晴大哥们不吝赐教.
#8
如何配置路径,请大哥们不吝赐教!!!
环境:Tomcat
文件目录如下:
%TOMCAT_HOME%\webapps\
mysample/login.html
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
//////////////////////login.html ////////////////////////
<HTML>
<HEAD>
</HEAD>
<BODY>
<form name="form1" method="post" action="/servlet/forky">
用户名:
<input type="text" name="username" size="15"><br><br>
密码:
<input type="password" name="password" size="15"><br><br>
<input type="submit" name="Submit" value="确定">
</form>
</BODY>
</HTML>
////////////////////////validate.jsp:////////////////////////
<%!
boolean isnotlogin=false;
%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username==null||password==null)
{
response.sendRedirect("error.jsp?errmsg=页面值传递错误,或者非法进入该页面(在validate.jsp中)");return;
}
%>
<jsp:useBean id="validatebean" scope="page" class="mypackage.validate"/>
<jsp:setProperty name="validatebean" property="username" param="username"/>
<jsp:setProperty name="validatebean" property="pwd" param="password"/>
</jsp:useBean>
<%
isnotlogin =validatebean.uservalidate();
if(!isnotlogin)
{
response.sendRedirect("error.jsp?errmsg=用户名或者口令错误!(在validate.jsp中)");return;
}
else
{
session.setAttribute("username",username);
%>
<jsp:forward page="servlet/bean">
<jsp:param name="username" value="<%=username%>"/>
</jsp:forward>
<%
}
%>
////////////////////////jb_aptech.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在jb_aptech.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在jb_aptech.jsp中)");return;
}
out.print("<br>);
out.print("欢迎");
out.print("<br>");
out.print("同学们");
out.print("<br>");
%>
////////////////////////teacher.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在teacher.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在teacher.jsp中)");return;
}
out.print("哈哈!");
out.print("<br>");
out.print("老师们");
out.print("<br>");
out.print("同,成功");
out.print("<br>");
out.print("努力");
////////////////////////error.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%String str=request.getParameter("errmsg");
String errmsg=new String(str.getBytes("ISO-8859-1"),"GBK");
%>
<html>
<head>
</head>
<body>
对不起,您的操作有误。请参考下列提示:<p>
<b><%=errmsg%></b>
</body>
</html>
////////////////////////web.xml:////////////////////////
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Micrcosystems,Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>forky</servlet-name>
<servlet-class>forky</servlet-class>
</servlet>
</web-app>
////////////////////////validate.java:////////////////////////
package mypackage;
public class validate
{
final String user1="jb";
final String password1="jb";
final String user2="aptech";
final String password2="aptech";
final String user3="teacher";
final String password3="student";
private String username="";
private String pwd="";
public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return this.username;
}
public void setPwd(String password)
{
pwd=password;//this.
}
public boolean uservalidate()
{
boolean temp =false;
if(username.equals(user1)&&pwd.equals(password1))
{
temp=true;
}
else if(username.equals(user2)&&pwd.equals(password2))
{
temp=true;
}
else if(username.equals(user3)&&pwd.equals(password3))
{
temp=true;
}
else
{
temp=false;
}
return temp;
}
}
////////////////////////forky.java:////////////////////////
package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class forky extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
HttpSession session=request.getSession(false);
if(session==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在servlet:forky中)");return;
}
String username1=(String)session.getAttribute("username");
String username2=request.getParameter("usrename");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在servlet:forky中)");return;
}
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
request.setAttribute("username",username2);
if(username2.equals("teacher"))
{
getServletConfig().getServletContext().getRequestDispatcher("/tercher.jsp").forward(request,response);
}
else
{
getServletConfig().getServletContext().getRequestDispatcher("/jb_aptech.jsp").forward(request,response);
}
}
}
环境:Tomcat
文件目录如下:
%TOMCAT_HOME%\webapps\
mysample/login.html
validate.jsp
jb_aptech.jsp
teacher.jsp
error.jsp
WEB-INF
web.xml
classes/
mypackage/validate.class
forky.class
//////////////////////login.html ////////////////////////
<HTML>
<HEAD>
</HEAD>
<BODY>
<form name="form1" method="post" action="/servlet/forky">
用户名:
<input type="text" name="username" size="15"><br><br>
密码:
<input type="password" name="password" size="15"><br><br>
<input type="submit" name="Submit" value="确定">
</form>
</BODY>
</HTML>
////////////////////////validate.jsp:////////////////////////
<%!
boolean isnotlogin=false;
%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
if(username==null||password==null)
{
response.sendRedirect("error.jsp?errmsg=页面值传递错误,或者非法进入该页面(在validate.jsp中)");return;
}
%>
<jsp:useBean id="validatebean" scope="page" class="mypackage.validate"/>
<jsp:setProperty name="validatebean" property="username" param="username"/>
<jsp:setProperty name="validatebean" property="pwd" param="password"/>
</jsp:useBean>
<%
isnotlogin =validatebean.uservalidate();
if(!isnotlogin)
{
response.sendRedirect("error.jsp?errmsg=用户名或者口令错误!(在validate.jsp中)");return;
}
else
{
session.setAttribute("username",username);
%>
<jsp:forward page="servlet/bean">
<jsp:param name="username" value="<%=username%>"/>
</jsp:forward>
<%
}
%>
////////////////////////jb_aptech.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在jb_aptech.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在jb_aptech.jsp中)");return;
}
out.print("<br>);
out.print("欢迎");
out.print("<br>");
out.print("同学们");
out.print("<br>");
%>
////////////////////////teacher.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%
String username1=(String)session.getAttribute("username");
if(username1==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在teacher.jsp中)");return;
}
String username2=request.getParameter("username");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在teacher.jsp中)");return;
}
out.print("哈哈!");
out.print("<br>");
out.print("老师们");
out.print("<br>");
out.print("同,成功");
out.print("<br>");
out.print("努力");
////////////////////////error.jsp:////////////////////////
<%@page language="java"%>
<%@page contentType="text/html;charset=gb2312"%>
<%String str=request.getParameter("errmsg");
String errmsg=new String(str.getBytes("ISO-8859-1"),"GBK");
%>
<html>
<head>
</head>
<body>
对不起,您的操作有误。请参考下列提示:<p>
<b><%=errmsg%></b>
</body>
</html>
////////////////////////web.xml:////////////////////////
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Micrcosystems,Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>forky</servlet-name>
<servlet-class>forky</servlet-class>
</servlet>
</web-app>
////////////////////////validate.java:////////////////////////
package mypackage;
public class validate
{
final String user1="jb";
final String password1="jb";
final String user2="aptech";
final String password2="aptech";
final String user3="teacher";
final String password3="student";
private String username="";
private String pwd="";
public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return this.username;
}
public void setPwd(String password)
{
pwd=password;//this.
}
public boolean uservalidate()
{
boolean temp =false;
if(username.equals(user1)&&pwd.equals(password1))
{
temp=true;
}
else if(username.equals(user2)&&pwd.equals(password2))
{
temp=true;
}
else if(username.equals(user3)&&pwd.equals(password3))
{
temp=true;
}
else
{
temp=false;
}
return temp;
}
}
////////////////////////forky.java:////////////////////////
package mypackage;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class forky extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
HttpSession session=request.getSession(false);
if(session==null)
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!(在servlet:forky中)");return;
}
String username1=(String)session.getAttribute("username");
String username2=request.getParameter("usrename");
if(!username1.equals(username2))
{
response.sendRedirect("error.jsp?errmsg=会话已经过期了!或者发生了不可预料的错误(在servlet:forky中)");return;
}
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
request.setAttribute("username",username2);
if(username2.equals("teacher"))
{
getServletConfig().getServletContext().getRequestDispatcher("/tercher.jsp").forward(request,response);
}
else
{
getServletConfig().getServletContext().getRequestDispatcher("/jb_aptech.jsp").forward(request,response);
}
}
}