最近做了一些图像处理的内容,闲暇时间搞了下jsp,终于把至少两个月之前的代码的bug找出来了...
具体内容我在之前一篇博文有介绍,主要是增加了数据库的部分。其实一样处理,获得输入的用户名,密码,然后判断是否需要在当前页面用javascipt处理下(比如注册肯定是需要的,起码两次密码输的要一样),然后跳转逻辑页面,对接数据库,进行增删查改,最后跳转相应的页面.
有几点需要注意:1,myeclipse的导入jar的包与eclipse稍有不同.
2,javascript处理和跳转逻辑页面的处理的区别
之后有空再用javabean重写一遍.
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>
用户登录
</title>
</head>
<body bgcolor="#e3e3e3">
<center>
<form action="check.jsp" method="post">
<table>
<caption>用户登录</caption>
<tr><td>用户名:</td><td><input type="text" name="username" size="20"/></td></tr>
<tr><td>密码:</td><td><input type="text" name="pwd" size="20"/></td></tr>
<tr><td><input type="submit" value="登录"/><td><input type="reset" value="重置"/>
</table>
</form>
如果您还没有注册,请单击<a href="register.jsp">这里</a>注册!
</body>
</center>
</html>
register.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%
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>注册页面</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">
-->
<script language="javascript">
function isValid(form)
{
if (form.username.value=="")
{
alert("用户名不能为空");
return false;
}
if (form.pwd.value!=form.pwd2.value)
{
alert("两次输入的密码不同!");
return false;
}
else if (form.pwd.value=="")
{
alert("用户密码不能为空!");
return false;
}
else return true;
}
</script>
</head>
<body>
<center>
<body bgcolor="#e3e3e3">
<h2>用户注册</h2>
<form action="check2.jsp" method="post" onSubmit="return isValid(this);">
<table>
<tr><td>用户名:</td><td><input type="text" name="username" size="20"/></td></tr>
<tr><td>输入密码:</td><td><input type="text" name="pwd" size="20"/></td></tr>
<tr><td>再次确认密码:</td><td><input type="text"name="pwd2" size="20"/></td><tr>
<tr><td><input type="submit" value="注册"/><td><input type="reset" value="重置"/>
</table>
</form>
</center>
<br>
</body>
</html>
check.jsp(判断用户登录)
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
<%
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 'check.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>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("username");
String pass=request.getParameter("pwd");
boolean flag=false;
PreparedStatement sql=null;
ResultSet rs=null;
Connection conn=null;
%>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3307/login";
String use = "root";
String password = "960404";
Class.forName(driver);
conn= DriverManager.getConnection(url,use,password);
sql =conn.prepareStatement("select * from student where username=? and password=?");
sql.setString(1,users);
sql.setString(2,pass);
rs=sql.executeQuery();
if (rs.next()) {
flag=true;
}
rs.close();
sql.close();
conn.close();
%>
<!-- 判断是否是正确的登录用户 -->
<% if (flag==true){ %>
<jsp:forward page="show.jsp"/>
<%}
else
if (flag==false){
%>
<jsp:forward page="login_fali.jsp"/>
<%} %>
</body>
</html>
check2.jsp(判断用户注册)
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
<%
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>检验注册页面</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>
<br>
<%
request.setCharacterEncoding("utf-8");
String users=request.getParameter("username");
String pass=request.getParameter("pwd");
%>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3307/login";
String use = "root";
String password = "960404";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,use,password);
PreparedStatement sql =conn.prepareStatement("insert into student(username,password)values(?,?)");
sql.setString(1,users);
sql.setString(2,pass);
int rtn=sql.executeUpdate();
sql.close();
conn.close();
%>
</body>
</html>
show.jsp(登录成功)
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%
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>登录成功</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>
登录成功. <br>
</body>
</html>
login_fail.jsp(注意还是要跳转回原来的登录页面的)
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
<%
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>登录失败</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>
<% out.println("登录失败");%>
<% response.setHeader("refresh","5;url=login.jsp");%>
</body>
</html>