第一篇写到了关于我对jsp modul 1的理解,这里我在写写关于我对jsp modul 2的理解。
jsp modul 2是jsp+javabean+servlet,基于jsp modul 1多了一个servlet,在modul 1中,是由jsp进行逻辑判断,然后调用java bean连接数据库,但是在modul 2中,则是由客户上传信息,由servlet调用不同的javabean,和数据库进行连接,随后把数据返回给servlet,servlet在调用jsp将信息返回给客户,让客户看到。这样做更便于java代码与页面的分离,下面我们在看个关于jsp modul 2的例子:
声明:我也是初学者,用我自己的理解写的代码与这篇文章,如果有误导您,或者不好的地方,请大家提出宝贵意见。
index.jsp,用于用户上传表单信息。
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'index.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>
<form action="chaxun" method="post">
//将页面上传至chaxun这个servlet
用户名<input type="text" name="uname">
<input type="submit" name="submit" value="查询">
</form>
</body>
</html>
chaxun.java提交的表单传递到这个servlet。
package cn.com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.bean.bean;
import cn.com.db.dbbean;
public class chaxun extends HttpServlet {
/**
* Constructor of the object.
*/
public chaxun() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
String name = request.getParameter("uname");
//从request中获取上传的uname
dbbean db = new dbbean();
//创建dbbean这个bean的对象
db.setUname(name);
//将name传入到dbbean这个bean
ArrayList al = db.Query(name);
//创建ArrayList的对象,且指向dbbean里这个方法
request.setAttribute("al",al);
request.getRequestDispatcher("/result.jsp").forward(request, response);
//将request转发到result.jsp这个界面
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
dbbean.java用于与数据库进行数据交换
package cn.com.db;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import cn.com.bean.bean;
public class dbbean {
Connection con =null;
Statement stmt = null;
ResultSet rs = null;
private String uname;
private String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "jdbc:microsoft:sqlserver://localhost:1433;database=people";
private String username = "kobe";
private String password = "1230123";
public dbbean(){
try {
Class.forName(driver);
}catch (ClassNotFoundException ce){
ce.printStackTrace();
}
try {
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
}catch (SQLException se){
se.printStackTrace();
}
}
public ArrayList Query(String name){
ArrayList al = new ArrayList();
bean b = new bean();
String sql = "select * from userpeople where name='"+uname+"';";
try {
rs = stmt.executeQuery(sql);
while (rs.next()){
b.setEmail(rs.getString("e_mail"));
b.setName(rs.getString("name"));
b.setPassword(rs.getString("password"));
al.add(b);
}
}catch (SQLException se){
se.printStackTrace();
}finally {
try {
rs.close();
stmt.close();
con.close();
}catch (SQLException se){
se.printStackTrace();
}
}
return al;
}
public String getUname(){
return uname;
}
public void setUname(String uname){
this.uname = uname;
}
}
bean.java从数据库提取后的信息,传递到这个bean。
package cn.com.bean;
public class bean {
private String email;
private String name;
private String password;
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
result.jsp最后的显示页面
<%@ page language="java" import="java.util.*,cn.com.bean.*" pageEncoding="gb2312"%>
<%
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 'result.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>
关于您的信息:
<table>
<tr>
<th>E-mail</th>
<th>用户名</th>
<th>密码</th>
</tr>
<%ArrayList al = (ArrayList)request.getAttribute("al");
for (int i=0;i<al.size();i++){
bean b = (bean)al.get(i); %>
<tr>
<td><%=b.getEmail() %></td>
<td><%=b.getName() %></td>
<td><%=b.getPassword() %></td>
</tr>
</table>
<%} %>
</body>
</html>