用戶登錄頁面
比較,觀察jsp和它轉成的servlet(*.java)。
說明:客戶端<form>提交至的頁面,還是本身。
//C:\Program Files\tomcat6\webapps\Shopping
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String action = request.getParameter("action");
if(action != null && action.equals("login")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if( !username.equals("admin") || !password.equals("admin") ) {
out.println("username or password not correct!");
return;
}
session.setAttribute("admin" , "admin");
response.sendRedirect("admin/AdminIndex.jsp");
}
%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="AdminLogin.jsp" method="post">
<input type="hidden" name="action" value="login"/>
<table border="1" align="center">
<tr>
<td>admin name:</td>
<td><input type="text" size="10" name="username"/></td>
</tr>
<tr>
<td>admin password:</td>
<td><input type="password" size="10" name="password"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="login"/><input type="reset" value="reset"/></td>
</tr>
</table>
</form>
</body>
</html>
//C:\Program Files\tomcat6\work\Catalina\localhost\Shopping\org\apache\jsp
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class AdminLogin_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=GB18030");//!!!!!!!!!!!!!!!!!
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
String action = request.getParameter("action");
if(action != null && action.equals("login")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if( !username.equals("admin") || !password.equals("admin") ) {
out.println("username or password not correct!");
return;
}
session.setAttribute("admin" , "admin");
response.sendRedirect("admin/AdminIndex.jsp");
}
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB18030\">\r\n");//!!!!!!!!!!!!!!!
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("<form action=\"AdminLogin.jsp\" method=\"post\">\r\n");
out.write("\t<input type=\"hidden\" name=\"action\" value=\"login\"/>\r\n");
out.write("<table border=\"1\" align=\"center\">\r\n");
out.write("\t<tr>\r\n");
out.write("\t\t<td>admin name:</td>\r\n");
out.write("\t\t<td><input type=\"text\" size=\"10\" name=\"username\"/></td>\r\n");
out.write("\t</tr>\r\n");
out.write("\t<tr>\r\n");
out.write("\t\t<td>admin password:</td>\r\n");
out.write("\t\t<td><input type=\"password\" size=\"10\" name=\"password\"/></td>\r\n");
out.write("\t</tr>\r\n");
out.write("\t<tr>\r\n");
out.write("\t\t<td></td>\r\n");
out.write("\t\t<td><input type=\"submit\" value=\"login\"/><input type=\"reset\" value=\"reset\"/></td>\r\n");
out.write("\t</tr>\r\n");
out.write("</table>\r\n");
out.write("</form>\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}