您的位置:首页 > Web前端 > JavaScript

JSP之登录,注册页面(一)

2015-11-12 22:50 519 查看

摘要:本系列会制作一个简单的需要JSP,servlet,oracle一起完成的登录,注册页面

1,首先需要一个登录的界面login.jsp

代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
<%
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 'login.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<script type="text/javascript">
function func(ac) {
form=document.getElementById("form1");
form.action=ac;
form.submit();
}
</script>
<body>
<form  id="form1">
登录名:<input type="text" name="name"/><br/>
密  码:<input type="password" name="password"/><br/>
<input type="button" value="登录" onclick="func('lotry')"/>
<input type="button" value="注册" onclick="func('regist.jsp')">
</form>

</body>
</html>


2,需要一个用来服务登录的servlet LoginEntry.java

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.userdata;
@WebServlet(urlPatterns={"/lotry"})
public class LoginEntry extends HttpServlet{
userdata dao;
HttpSession hs;
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
String name=req.getParameter("name");
String pass=req.getParameter("password");
dao=userdata.getdao();
if(dao.login(name, pass)){

req.getRequestDispatcher("success.jsp").forward(req, res);
}else{
req.getRequestDispatcher("failed.jsp").forward(req, res);
}
}

}


3,需要一个登录成功然后跳转的页面

<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
<%
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 'success.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
恭喜你,成功登录
</body>
</html>


4,需要一个登录失败的跳转页面

<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
<%
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 'failed.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
登录失败,<a href="login.jsp">请重新登录</a>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: