关于JSP的一点疑问小结
程序员文章站
2024-02-18 22:27:58
希望谁能帮我看一看问题出在哪?谢谢! register.html部分: 复制代码 代码如下:
希望谁能帮我看一看问题出在哪?谢谢!
register.html部分:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<br>
<form action="register.jsp" method = "post" name = "frm">
用户名:<input type = "text" name = "in_username"><br>
密码:<input type = "password" name = "in_password"><br>
<input type = "submit" name = "submit" value = "提交">
</form>
</body>
</html>
register.jsp部分:
<%@ page language="java" import = java.util.* pageencoding="iso-8859-1"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>">
<title>my jsp 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv = "content-type" content = "text/html;charset = gb2312">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<%! boolean isnotlogin = false;%>
<%
string username = request.getparameter("username");
string password = request.getparameter("password");
if(username == null || password == null)
{
response.sendredirect("error.jsp");
return;
}
%>
<jsp:usebean id="person" scope = "page" class = "mypack.register">
<jsp:setproperty name = "person" property = "username" param = "username"/>
<jsp:setproperty name = "person" property = "pwd" param = "password"/>
</jsp:usebean>
<%
isnotlogin = person.judge();
if(!isnotlogin)
{
response.sendredirect("error.jsp");
return;
}
else
{
session.setattribute("username", request.getparameter("username"));
%>
<jsp:forward page = "sbmt">
<jsp:param name = "username" value = "<%=username%>"/>
</jsp:forward>
<%
}
%>
</body>
</html>
register.java(javabean)部分:
package mypack;
public class register {
private string username = "";
private string pwd = "";
public void setusername(string nm)
{
this.username = nm;
}
public string getusername()
{
return this.username;
}
public void setpwd(string pd)
{
this.pwd = pd;
}
public string getpwd()
{
return this.pwd;
}
public boolean judge()
{
boolean temp = false;
if(username.equals("teacher") && pwd.equals("teacher"))
{
temp = true;
}
return temp;
}
}
sbmt.java(servlet)部分:
package mypack;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
public class sbmt extends httpservlet {
private static final long serialversionuid = 1l;
public sbmt() {
super();
}
public void destroy() {
super.destroy();
}
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html");
printwriter out = response.getwriter();
out
.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">");
out.println("<html>");
out.println(" <head><title>a servlet</title></head>");
out.println(" <body>");
out.print(" this is ");
out.print(this.getclass());
out.println(", using the get method");
out.println(" </body>");
out.println("</html>");
out.flush();
out.close();
}
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
httpsession session = request.getsession(false);
if(session == null)
{
response.sendredirect("error.jsp");
return;
}
string usernameone = (string)session.getattribute("username");
string usernametwo = request.getparameter("username");
if(!usernameone.equals(usernametwo))
{
response.sendredirect("error.jsp");
return;
}
response.setcontenttype("text/html;charset = gbk");
request.setattribute("username", usernametwo);
if(usernametwo.equals("teacher"))
{
response.sendredirect("teacher.jsp");
return;
}
else
{
response.sendredirect("error.jsp");
return;
}
}
}
}
register.html部分:
复制代码 代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>register.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<br>
<form action="register.jsp" method = "post" name = "frm">
用户名:<input type = "text" name = "in_username"><br>
密码:<input type = "password" name = "in_password"><br>
<input type = "submit" name = "submit" value = "提交">
</form>
</body>
</html>
register.jsp部分:
复制代码 代码如下:
<%@ page language="java" import = java.util.* pageencoding="iso-8859-1"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>">
<title>my jsp 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv = "content-type" content = "text/html;charset = gb2312">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<%! boolean isnotlogin = false;%>
<%
string username = request.getparameter("username");
string password = request.getparameter("password");
if(username == null || password == null)
{
response.sendredirect("error.jsp");
return;
}
%>
<jsp:usebean id="person" scope = "page" class = "mypack.register">
<jsp:setproperty name = "person" property = "username" param = "username"/>
<jsp:setproperty name = "person" property = "pwd" param = "password"/>
</jsp:usebean>
<%
isnotlogin = person.judge();
if(!isnotlogin)
{
response.sendredirect("error.jsp");
return;
}
else
{
session.setattribute("username", request.getparameter("username"));
%>
<jsp:forward page = "sbmt">
<jsp:param name = "username" value = "<%=username%>"/>
</jsp:forward>
<%
}
%>
</body>
</html>
register.java(javabean)部分:
复制代码 代码如下:
package mypack;
public class register {
private string username = "";
private string pwd = "";
public void setusername(string nm)
{
this.username = nm;
}
public string getusername()
{
return this.username;
}
public void setpwd(string pd)
{
this.pwd = pd;
}
public string getpwd()
{
return this.pwd;
}
public boolean judge()
{
boolean temp = false;
if(username.equals("teacher") && pwd.equals("teacher"))
{
temp = true;
}
return temp;
}
}
sbmt.java(servlet)部分:
package mypack;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
public class sbmt extends httpservlet {
private static final long serialversionuid = 1l;
public sbmt() {
super();
}
public void destroy() {
super.destroy();
}
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html");
printwriter out = response.getwriter();
out
.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">");
out.println("<html>");
out.println(" <head><title>a servlet</title></head>");
out.println(" <body>");
out.print(" this is ");
out.print(this.getclass());
out.println(", using the get method");
out.println(" </body>");
out.println("</html>");
out.flush();
out.close();
}
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
httpsession session = request.getsession(false);
if(session == null)
{
response.sendredirect("error.jsp");
return;
}
string usernameone = (string)session.getattribute("username");
string usernametwo = request.getparameter("username");
if(!usernameone.equals(usernametwo))
{
response.sendredirect("error.jsp");
return;
}
response.setcontenttype("text/html;charset = gbk");
request.setattribute("username", usernametwo);
if(usernametwo.equals("teacher"))
{
response.sendredirect("teacher.jsp");
return;
}
else
{
response.sendredirect("error.jsp");
return;
}
}
}
}