摘要:本系列会制作一个简单的需要JSP,servlet,oracle一起完成的登录,注册页面
1,首先需要一个登录的界面login.jsp
代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'login.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 <script type="text/javascript"> 25 function func(ac) { 26 form=document.getElementById("form1"); 27 form.action=ac; 28 form.submit(); 29 } 30 </script> 31 <body> 32 <form id="form1"> 33 登录名:<input type="text" name="name"/><br/> 34 密 码:<input type="password" name="password"/><br/> 35 <input type="button" value="登录" onclick="func('lotry')"/> 36 <input type="button" value="注册" onclick="func('regist.jsp')"> 37 </form> 38 39 </body> 40 </html>
2,需要一个用来服务登录的servlet LoginEntry.java
1 package servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.ServletRequest; 7 import javax.servlet.ServletResponse; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpSession; 13 14 import dao.userdata; 15 @WebServlet(urlPatterns={"/lotry"}) 16 public class LoginEntry extends HttpServlet{ 17 userdata dao; 18 HttpSession hs; 19 @Override 20 protected void service(HttpServletRequest req, HttpServletResponse res) 21 throws ServletException, IOException { 22 req.setCharacterEncoding("GBK"); 23 String name=req.getParameter("name"); 24 String pass=req.getParameter("password"); 25 dao=userdata.getdao(); 26 if(dao.login(name, pass)){ 27 28 req.getRequestDispatcher("success.jsp").forward(req, res); 29 }else{ 30 req.getRequestDispatcher("failed.jsp").forward(req, res); 31 } 32 } 33 34 35 }
3,需要一个登录成功然后跳转的页面
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'success.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 恭喜你,成功登录 27 </body> 28 </html>
4,需要一个登录失败的跳转页面
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'failed.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 登录失败,<a href="login.jsp">请重新登录</a> 27 </body> 28 </html>