一、目的
1)理解JSP的基本工作原理及执行过程;
2)熟悉JSP中常用的页面元素,理解其基本用法;
3)熟悉JSP的主要内置对象,掌握5个重要内置对象的用法;
4)学会使用JSP技术开发动态网页,构建简单的Web应用
二、原理
1、JSP的基本工作原理及执行过程,JSP与Servlet之间的关系;
2、JSP中的常用页面元素及其用法;
3、JSP中的5个重要内置对象及其用法;
三、步骤、数据记录及处理
1、创建用户注册页面,并使用JSP技术实现后台处理,将注册信息保存到数据库,在注册成功后自动跳转到登录页面;
(1)注册:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Register</title>
</head>
<body>
<form action="registeract.jsp" method="post">
Please enter your registration information:<br>
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Email : <input type="email" name="email"><br>
<input type="submit" value="Register">
</form>
</body>
</html>
(2)注册成功后自动跳转到登录页面:
<%@ page language="java" pageEncoding="utf-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="driver.Driver" %>
<%@ page import="javax.naming.NamingException" %>
<%
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
Connection conn = null;
PreparedStatement stat = null;
try {
conn = Driver.getConnection();
stat = conn.prepareStatement("INSERT into guestbook.user(username, password, email) values(?,?,?)");
stat.setString(1,username);
stat.setString(2,password);
stat.setString(3,email);
stat.executeUpdate();
response.sendRedirect("login.jsp");
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} finally {
Driver.close(stat,conn);
}
%>
(3)运行结果:
首先输入注册信息:
自动跳转到登录页面:
注册的信息自动保存到数据库中:
2、创建用户登录页面,并使用JSP技术实现后台处理,查询数据库,验证用户名和密码,根据验证结果跳转到成功页面或失败页面;
(1)登录
(1)<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="loginact.jsp" method="post">
Please enter you username and password to log in:<br>
Username:<input name="username" type="text"><br>
Password:<input name="password" type="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
(2)登录成功转到成功页面,登录失败转入失败页面:
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="driver.Driver" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="VO.VO" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Boolean state = false;
String username = request.getParameter("username");
String password = request.getParameter("password");
ResultSet rs = null;
PreparedStatement stat = null;
Connection con = null;
try {
con = Driver.getConnection();
stat = con.prepareStatement("SELECT * FROM user WHERE username = ?");
stat.setString(1,username);
rs = stat.executeQuery();
rs.next();
String Temp = rs.getString(2);
if(Temp.equals(password))
{
response.sendRedirect("success.html");
} else {
response.setCharacterEncoding("GBK");
RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/login.jsp ");
PrintWriter pw = response.getWriter();
pw.println("<div style='color:red'>Incorrect username or password!</div>");
rd.include(request,response);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} finally {
Driver.close(rs,stat,con);
}
%>
(3)运行结果:
登录页面:
登录成功页面:
登录失败页面:
3.使用JSP技术改造留言本应用,用JSP实现添加留言、显示所有留言等核心处理功能;
(1)创建数据库,用于存放留言信息:
creat table guestbook.book (
name varchar(20) NOT NULL,
title varchar (45) NULL,
content varchar (100) NULL,
primary key KEY(name));
(2)添加留言页面:
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="driver.Driver" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>WriteMessage</title>
</head>
<body>
<form action="msgact.jsp" method="post">
<h1>Message Book</h1><br>
Name:
<input name="name" type="text"><br>
Title:
<input name="title" type="text"><br>
Content:
<textarea name="content" id="content" cols="60" rows="5"></textarea><br>
<input type="submit" name="submit" value="Submit" >
</form>
<a href="displaymeg.jsp">View all message</a>
(3)将留言存入数据库中:
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="driver.Driver" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String title = request.getParameter("title");
String content = request.getParameter("content");
PreparedStatement ps = null;
Connection conn = null;
conn = Driver.getConnection();
ps = conn.prepareStatement("INSERT into guestbook.book(name, title, content) values(?,?,?)");
ps.setString(1,name);
ps.setString(2,title);
ps.setString(3,content);
ps.executeUpdate();
response.sendRedirect("displaymeg.jsp");
Driver.close(ps,conn);
%>
(4)显示所有留言:
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="driver.Driver" %>
<%@ page import="VO.SVO" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="VO.VO" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Messages</title>
</head>
<body>
<a href="msg.jsp">Write Message</a>
<%
int count = 0;
Integer scount = (Integer)application.getAttribute("scount");
if( scount != null ) count = scount.intValue( );
count++;
application.setAttribute("scount", new Integer(count));
%>
There are <%= count %> people visited this page<br><br>
<table border="1"width="50%">
<%
List<SVO> list = showAllMessage();
VO userVO = (VO) session.getAttribute("JudgeLogin");
if (userVO == null)
{
response.sendRedirect("login.jsp");
PrintWriter printWriter = response.getWriter();
printWriter.flush();
printWriter.println("<div style='color:red'>Log in to view all message!</div>");
}
%>
<tr>
<th>name</th>
<th>title</th>
<th>content</th>
</tr>
<%
for(int x = 0 ; x < list.size() ; x ++) {
%>
<tr>
<td><% out.println(list.get(x).getName()); %></td>
<td><% out.println(list.get(x).getTitle()); %></td>
<td><% out.println(list.get(x).getContent()); %></td>
</tr>
<%
}
%>
</table>
<%!
public ArrayList<SVO> showAllMessage() {
List<SVO> list = new ArrayList<SVO>();
SVO record = null;
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
String showSql = "SELECT * FROM guestbook.book";
try {
conn = Driver.getConnection();
ps = conn.prepareStatement(showSql);
rs = ps.executeQuery();
while (rs.next()) {
record = new SVO(rs.getString(1), rs.getString(2), rs.getString(3));
list.add(record);
}
Driver.close(rs, ps, conn);
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
return (ArrayList<SVO>) list;
}
%>
</body>
</html>
4、为留言本应用添加访问控制功能,只有登录用户才能查看留言;当未登录用户请求留言列表页面时,自动跳转到登录页面。
(1)当用户没有登录,查看所有留言时到登录页面login.jsp,当用户登录成功时显示留言内容。
没有登录时跳转到login.jsp,核心代码:
<%
List<SVO> list = showAllMessage();
VO userVO = (VO) session.getAttribute("JudgeLogin");
if (userVO == null)
{
response.sendRedirect("login.jsp");
PrintWriter printWriter = response.getWriter();
printWriter.flush();
printWriter.println("<div style='color:red'>Log in to view all message!</div>");
}
%>