關於JSP的一點疑問小結

來源:互聯網
上載者:User

希望誰能幫我看一看問題出在哪?謝謝!
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;
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.