目录
前言
一、目的
二、开始
1.需要工具
2.环境
3.代码
总结
前言
提示:JSP的日常一些Demo案例!
提示:以下是本篇文章正文内容,下面案例可供参考
一、目的
为了实现通过页面对数据库进行增 查功能
二、开始
1.需要工具
- eclipse
- java环境
- tomcat
- mysql-connect-jdbc
2.环境
- eclipse能正常打开
- jsp的默认项目能正常运行
3.代码
本人的数据库是mysql5.+
jdbc工具是5.0.8
这是login.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录界面</title>
</head>
<body>
<form method="POST" action="checkLogin.jsp">
<table cellspacing="" cellpadding="">
<tr>
<td>账号:</td>
<td>
<input type="text" name="username" id="username" value="" />
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="password" id="username" value="" />
</td>
</tr>
<tr>
<td>角色:</td>
<td>
<input type="radio" name="role" id="" value="管理员" /><label>管理员</label>
<input type="radio" name="role" id="" value="普通用户" checked=""/><label>普通用户</label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" name="" id="" value="登录" />
<input type="reset" name="" id="" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
checkLogin.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.sql.*"
%>
<!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=utf-8">
<title>验证账号密码</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String role=request.getParameter("role");
%>
<%
//插入数据库
try {
Class.forName("com.mysql.jdbc.Driver");
out.println("加载驱动类成功<br/>");
String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
Connection conn = DriverManager.getConnection(url);
if(conn!=null){
out.println("数据库连接成功<br/>");
}
String sql="select * from users where username=? and password =? and role=?";
//"insert into t_email values('?','?','?','?','?','?','?','?')"
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,username );
ps.setString(2,password );
ps.setString(3,role );
ResultSet result=ps.executeQuery();
if(result.next()){
System.out.print("登录成功<br/>");
session.setAttribute("username", username);
response.sendRedirect("addEmail.jsp");
}else{
System.out.println("登录失败");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
} catch (Exception e) {
out.println("找不到驱动类<br/>"+e);
}
%>
</body>
</html>
这是logined.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>验证账号密码</title>
</head>
<body>
<%
if(session.getAttribute("username")==null||session.getAttribute("username").equals("")){
out.println("<script type='text/javascript'> alert('请先登录!'); location.href='login.jsp'; </script>");
//response.setHeader("refresh", "2;url='login.jsp'");
}else{
}
%>
</body>
</html>
这是addEmail.jsp文件 前端数据输入
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>登录信息</title>
</head>
<body>
<form action="saveEmail.jsp" method="post">
<table cellspacing="" cellpadding="">
<tr>
<td>
选择您的雅虎邮箱:
<input type="text" name="mail" id="" value="" />
<span style="color: red;">@</span>
<SELECT name="domain">
<OPTION value="1" name="domain">yahoo.cn</OPTION>
<OPTION value="2" name="domain">yahoo.com.cn</OPTION>
</SELECT>
</td>
</tr>
<tr>
<td>
密码:
<input type="password" name="passwd1" id="" value="" size="6"/>
</td>
</tr>
<tr>
<td>
再次输入密码:
<input type="password" name="passwd2" id="" value="" size="6"/>
</td>
</tr>
<tr>
<td>
真实姓名:
<input type="text" name="name" id="" value="" />
</td>
</tr>
<tr>
<td>
出生日期:
<input type="text" name="birthday" id="" value="" />
</td>
</tr>
<tr>
<td>
性别:
<input type="radio" name="gender" id="" value="男" checked=""/>男
<input type="radio" name="gender" id="" value="女" />女
</td>
</tr>
<tr>
<td>杂志订阅:
<input type="checkbox" name="habbit" id="" value="旅游" />旅游
<input type="checkbox" name="habbit" id="" value="财经" />财经
<br />
<input type="checkbox" name="habbit" id="" value="汽车" />汽车
<input type="checkbox" name="habbit" id="" value="音乐" />音乐
</td>
</tr>
<tr>
<td>雅虎服务条款
<textarea name="tips" rows="5" cols="50">雅虎中国对于任何包含于、经由、或联结、下载或从任何与本网站有关服务(以下简称「服务」)所获得之资讯、内容或广告(以下简称「资料」),您于此接受并承认信赖任何「资料」所生之风险应自行承担。雅虎中国,有权但无此义务,改善或更正在「服务」或「资料」任何部分之错误或疏失。</textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" id="submit" value="同意服务条款并提交" style="width: 200px;"/ >
<input type="reset" name="reset" id="reset" value="清空" />
</td>
</tr>
</table>
</form>
</body>
</html>
saveEmail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.util.*"
import="java.sql.*" %>
<!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=utf-8">
<title>插入数据页面</title>
<style type="text/css">
table th{
width:100px;
}
</style>
</head>
<body>
<%
//数据呈现
request.setCharacterEncoding("utf-8");
String user=request.getParameter("mail");
String domains=request.getParameter("domain");
String domain="";
if(domains.equals("1")){
domain="yahoo.cn";
}else if(domains.equals("2")){
domain="yahoo.com.cn";
}
String pwd=request.getParameter("passwd1");
String confirm_pwd=request.getParameter("passwd2");
String realname=request.getParameter("name");
String birthday=request.getParameter("birthday");
String sex=request.getParameter("gender");
String[] habbit=request.getParameterValues("habbit");
String tips=request.getParameter("tips");
String habbits="";
for(int i=0;i<habbit.length;i++){
habbits+=habbit[i];
if(i!=habbit.length-1){
habbits+=",";
}
}
%>
<%
//插入数据库
try {
Class.forName("com.mysql.jdbc.Driver");
out.println("加载驱动类成功<br/>");
String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
Connection conn = DriverManager.getConnection(url);
if(conn!=null){
out.println("数据库连接成功<br/>");
}
String sql="insert into t_email values(?,?,?,?,?,?,?,?)";
//"insert into t_email values('?','?','?','?','?','?','?','?')"
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,user );
ps.setString(2,domains );
ps.setString(3,pwd );
ps.setString(4,realname );
ps.setString(5,birthday );
ps.setString(6,sex );
ps.setString(7,habbits );
ps.setString(8,tips );
int result=ps.executeUpdate();
if(result!=0){
out.print("成功插入<br/>");
%>
<table >
<tr>
<th>邮箱</th>
<td><%=user %></td>
</tr>
<tr>
<th>邮箱后缀</th>
<td><%=domain %></td>
</tr>
<tr>
<th>密码</th>
<td><%=pwd %></td>
</tr>
<tr>
<th>确认密码</th>
<td><%=confirm_pwd %></td>
</tr>
<tr>
<th>真实姓名</th>
<td><%=realname %></td>
</tr>
<tr>
<th>生日</th>
<td><%=birthday %></td>
</tr>
<tr>
<th>性别</th>
<td><%=sex %></td>
</tr>
<tr>
<th>爱好</th>
<td>
<%
out.println(habbits);
%>
</td>
</tr>
<tr>
<th>条款</th>
<td width="300px"><%=tips %></td>
</tr>
</table>
<%
response.setHeader("Refresh", "3;url=selectEmail.jsp");
}else{
out.print("插入失败<br/>");
}
} catch (Exception e) {
out.println("找不到驱动类<br/>"+e);
}
%>
</body>
</html>
selectEmail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.sql.*"
%>
<!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=utf-8">
<title>查询信息</title>
</head>
<body>
<table >
<tr>
<td>用户名:</td>
<td>邮箱:</td>
<td>真实名字:</td>
<td>生日:</td>
<td>性别:</td>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
Connection conn = DriverManager.getConnection(url);
String sql="select * from t_email";
Statement sm=conn.createStatement();
//System.out.println(sql);
ResultSet result=sm.executeQuery(sql);
result.last();
int count=result.getRow();
result.beforeFirst();
while(result.next()){
String username=result.getString("username");
String domain=result.getString("domain");
String realname=result.getString("realname");
String birthday=result.getString("birthday");
String sex=result.getString("sex");
%>
<tr>
<td>
<a href="detail_info.jsp?selectUserName=<%=username%>" target="_blank" rel="external nofollow" >
<%=username %>
</a>
</td>
<td><%=domain %></td>
<td><%=realname %></td>
<td><%=birthday %></td>
<td><%=sex %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
detail_info.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.sql.*"
%>
<!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=utf-8">
<title>查询信息</title>
</head>
<style type="text/css">
.info_table{
text-algin:center;
margin:10% 30%;
}
.info_table table tr td{
width:500px;
}
</style>
<body>
<div class="info_table">
<table >
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/yahoodb?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";
Connection conn = DriverManager.getConnection(url);
String selectUserName=request.getParameter("selectUserName");
String sql="select * from t_email where username='"+selectUserName+"'";
Statement sm=conn.createStatement();
ResultSet result=sm.executeQuery(sql);
result.last();
int count=result.getRow();
result.beforeFirst();
while(result.next()){
String username=result.getString("username");
String domain=result.getString("domain");
String password=result.getString("pwd");
String realname=result.getString("realname");
String birthday=result.getString("birthday");
String sex=result.getString("sex");
String order=result.getString("order");
String description=result.getString("description");
%>
<tr>
<th>用户名:</th>
<td><%=username %></td>
</tr>
<tr>
<th>邮箱:</th>
<td><%=domain %></td>
</tr>
<tr>
<th>密码:</th>
<td><%=password %></td>
</tr>
<tr>
<th>真实名字:</th>
<td><%=realname %></td>
</tr>
<tr>
<th>生日:</th>
<td><%=birthday %></td>
</tr>
<tr>
<th>性别:</th>
<td><%=sex %></td>
</tr>
<tr>
<th>喜好:</th>
<td><%=order %></td>
</tr>
<tr>
<th>描述:</th>
<td><%=description %></td>
</tr>
<%
}
%>
</table>
</div>
</body>
</html>
总结
实现了登录根据数据库中表的信息来登录,并对数据库进行 select insert操作!!