forward()方法突显的是原先Servlet的地点

新建表:

两种转向的分别:

addMessage.jsp

  admin:id,name ,password                  <pk>id

  forward()

代码;

平素在表admin中添加一个社团者:1,hongten,132

  request.setAttribute(“message”,
message);
  request.getRequestDispatcher(“/addResult.jsp”).forward(request,
response);

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ include file=”/header.jsp”%>
<style>
* { font-family: “宋体”; font-size: 14px }
</style>
<script type=”text/javascript”
src=”<%=context%>/js/validation-framework.js”></script>
<script type=”text/javascript”
src=”<%=context%>/fckeditor/fckeditor.js”></script>
<p align=”center”>
 请您输入留言
</p>
<p align=”center”>
 <a
href=”<%=context%>/servlet/getMessages”>查看留言</a>
</p>
<form id=”form1″ name=”form1″ method=”post”
 action=”<%=context%>/servlet/addMessage”   

新建web 项目:

  sendRedirect()

 

在WebRoot目录先新建2个文本夹:admin

  response.sendRedirect(“/guestbook6/getMessages.jsp”);

*****************************************************************************

login.jsp

 

*****************************************************************************

代码;
<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ include file=”/header.jsp”%>
<style>
* { font-family: “宋体”; font-size: 14px }
</style>
<script type=”text/javascript”
src=”${ctx}/js/validation-framework.js”></script>
<p align=”center”>
 请您输入用户名与密码
</p>
<form id=”form2″ name=”form2″
method=”post” action=”${ctx}/admin/user” onsubmit=”return
doValidate(this)”>
 <input type=”hidden” name=”q” value=”login”>
 <table width=”500″  border=”0″ align=”center”>
  <tr><td width=”100″>用户名:</td>
  <td width=”400″>
  <input name=”username” type=”text” id=”username” size=”20″
></td>
  </tr>
  <tr><td>密码:</td> <td>
  <input name=”password” type=”password” id=”password”
size=”20″></td>
  </tr>
  <tr>
  <td></td><td>
    <input type=”submit” name=”Submit” value=”提交” >
    <input type=”reset” name=”Reset” value=”重置” >
  </td></tr>
 </table>
</form>
<%@ include file=”/footer.htm”%>

区别:

*****************************************************************************

————————————-Hongten————————————————

一 、forward可以读取转向前request对象通过setAttribute()方法所设定的属性值,sendRedirect()不能够;

 证实:<%=context%>/servlet/addMessage中的<%=context%>那一个是赢得全套web项目的                                 

loginFail.jsp

② 、转向后,forward()方法突显的是原来Servlet的地方,即地址不变,而sendRedirect(“*****”)方法要成为*****地址;

的名字,如项目名称为:guestbook6,那么action=”<%=context%>/servlet/addMessage”                                 

代码;

叁 、forward()方法转化的速度要快,较sendRedirect()方法;

就等价于action=”guestbook6/servlet/addMessage”,使用那种格局的补益在那边吗?

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ include file=”/header.jsp”%>
<html>
<head>
<title>login fail</title>
</head>
<body>
抱歉,输入的用户名与密码不科学。<br>
<a href=”${ctx}/admin/login.jsp”>重新登录</a>
</body>
</html>

④ 、sendRedirect()方法能够转正到web应用之外的页面可能网站,

就在于对于修改项目名称的左右逢源,比如:今后,大家觉得大家务必得修改大家的项目名称,

————————————-Hongten————————————————

  如:response.sendRedirect(”
http://www.cnblogs.com/hongten/ “);

如修改为:guestbook7,假设大家不是用的是那种方法的话,当然也足以,只是大家要修改全数涉及到

AdminUserServlet.java

  而forward()是不得以的; 

项目名称的地点,那不免给大家造成很大的时刻支出。可是如果大家利用那种方法,即使大家修改了项目名称,

代码;

addMessage.jsp

丝毫不会潜移默化大家的类其他运维。那就是她的功利之所在了。

package webbook.guestbook;
import java.io.IOException;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import webbook.util.*;
import org.apache.commons.dbutils.handlers.MapListHandler;

代码:

可以大大给大家节约时间。

public class AdminUserServlet extends HttpServlet {
 private static final long serialVersionUID = 5801558969966197290L;

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ include file=”/header.jsp”%>
<style>
* { font-family: “宋体”; font-size: 14px }
</style>
<script type=”text/javascript”
src=”<%=context%>/js/validation-framework.js”></script>
<script type=”text/javascript”
src=”<%=context%>/fckeditor/fckeditor.js”></script>
<p align=”center”>
 请您输入留言
</p>
<p align=”center”>
 <a
href=”<%=context%>/servlet/getMessages”>查看留言</a>
</p>
<form id=”form1″ name=”form1″ method=”post”
 action=”<%=context%>/servlet/addMessage”
 onsubmit=”return doValidate(this)”>
 <table width=”650″ height=”400″ border=”0″ align=”center”>
  <tr>
   <td width=”150″>姓名:</td>
   <td width=”500″>
    <input name=”name” type=”text” id=”name” size=”40″ maxlength=”20″
/>
   </td>
  </tr>
  <tr>
   <td>E-Mail:</td>
   <td>
    <input name=”email” type=”text” id=”email” size=”40″
maxlength=”40″ />
   </td>
  </tr>
  <tr>
   <td>电话:</td>
   <td>
    <input name=”phone” type=”text” id=”phone” size=”40″
maxlength=”20″ />
   </td>
  </tr>
  <tr>
   <td>主题:</td>
   <td>
    <input name=”title” type=”text” id=”title” size=”80″
maxlength=”80″ />
   </td>
  </tr>
  <tr>
   <td valign=”top”>内容:</td>
   <td>
    <script type=”text/javascript”>
    var oFCKeditor = new FCKeditor(“content”);
    oFCKeditor.BasePath = ‘<%=context%>/fckeditor/’ ;
    oFCKeditor.Height = 300 ;
    oFCKeditor.ToolbarSet = ‘Basic’;
    oFCKeditor.Create() ;
    </script>
   </td>
  </tr>
  <tr>
   <td></td>
   <td>
    <input type=”submit” name=”Submit” value=”提交” />
    <input type=”reset” name=”Reset” value=”重置” />
   </td>
  </tr>
 </table>
</form>
<%@ include file=”/footer.htm”%>

*****************************************************************************

 public void login(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String message = “”;
  String username = request.getParameter(“username”);
  String password = request.getParameter(“password”);

OracleUtil.java

*****************************************************************************

  if (StringUtil.validateNull(username)) {
   message = “对不起,姓名不可以为空,请您再也输入!<br>”;
  } else if (StringUtil.validateNull(password)) {
   message = “对不起,密码不或许为空,请您再一次输入!<br>”;
  } else {
   String param[] = { username, password };
   OracleUtil db = new OracleUtil(“java:/comp/env/jdbc/oracleds”);
   db.init();
   List result = (List) db.query(“select id from admin where username=?
and password=?”, param,
     new MapListHandler());
   if (result.size() == 0) {
    message = “对不起,用户名大概密码错误”;
    request.setAttribute(“guesbook.admin.login.message”, message);
    request.getRequestDispatcher(“/admin/loginFail.jsp”).forward(request,
response);
   } else {
    HttpSession session = request.getSession();
    session.setAttribute(“guesbook.admin.username”, username);
    response.sendRedirect(request.getContextPath()+”/admin/secure/manage?q=list”);
   }
  }
 }

代码:

*****************************************************************************

 public void logout(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  HttpSession session = request.getSession();
  session.removeAttribute(“guesbook.admin.username”);
  response.sendRedirect(request.getContextPath()+”/admin/login.jsp”);  
 }

package webbook.util;

                  
 onsubmit=”return doValidate(this)”>
 <table width=”650″ height=”400″ border=”0″ align=”center”>
  <tr>
   <td width=”150″>姓名:</td>
   <td width=”500″>
    <input name=”name” type=”text” id=”name” size=”40″ maxlength=”20″
/>
   </td>
  </tr>
  <tr>
   <td>E-Mail:</td>
   <td>
    <input name=”email” type=”text” id=”email” size=”40″
maxlength=”40″ />
   </td>
  </tr>
  <tr>
   <td>电话:</td>
   <td>
    <input name=”phone” type=”text” id=”phone” size=”40″
maxlength=”20″ />
   </td>
  </tr>
  <tr>
   <td>主题:</td>
   <td>
    <input name=”title” type=”text” id=”title” size=”80″
maxlength=”80″ />
   </td>
  </tr>
  <tr>
   <td valign=”top”>内容:</td>
   <td>
    <script type=”text/javascript”>
    var oFCKeditor = new FCKeditor(“content”);
    oFCKeditor.BasePath = ‘<%=context%>/fckeditor/’ ;
    oFCKeditor.Height = 300 ;
    oFCKeditor.ToolbarSet = ‘Basic’;
    oFCKeditor.Create() ;
    </script>
   </td>
  </tr>
  <tr>
   <td></td>
   <td>
    <input type=”submit” name=”Submit” value=”提交” />
    <input type=”reset” name=”Reset” value=”重置” />
   </td>
  </tr>
 </table>
</form>
<%@ include file=”/footer.htm”%>

 public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String method = request.getParameter(“q”);
  if (method != null && method.equals(“login”)) {
   login(request, response);
  } else {
   logout(request, response);
  }
 }

import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

AddMessageServlet.java

 public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  doGet(request, response);
 }
}

public class OracleUtil {
 private String dataSourceName;
 private DataSource ds;

代码:

————————————-Hongten————————————————

 public OracleUtil(String dataSourceName) {
  this.dataSourceName = dataSourceName;  
 }

package webbook.guestbook;

AuthenticationFilter.java

 public OracleUtil() {  
 }

import java.io.IOException;
import
java.text.SimpleDateFormat;
import
javax.servlet.ServletException;
import javax.servlet.http.*;
import webbook.util.*;

代码:

 public void setDataSourceName(String dataSourceName) {
  this.dataSourceName = dataSourceName;
 }

public class AddMessageServlet extends
HttpServlet {
 private static final long
serialVersionUID = -8349454122547148005L;

package webbook.guestbook;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

 public void init() {
  Context initContext;
  try {
   initContext = new InitialContext();
   ds = (DataSource) initContext.lookup(dataSourceName);
  } catch (NamingException e) {
   e.printStackTrace();
  }
 }

 public void doPost(HttpServletRequest
request, HttpServletResponse response) throws ServletException,
IOException {
  String sql = “insert into guestbook
(id,name,email,phone,title,content,time)
values(gb_seq.nextval,?,?,?,?,?,?)”;
  int result = 0;
  String message = “”;
  request.setCharacterEncoding(“utf-8”);
  String name =
request.getParameter(“name”);
  String title =
request.getParameter(“title”);

public class AuthenticationFilter implements Filter {
 String url = “/”;

 public int update(String sql, String[] param) {
  int result = 0;
  QueryRunner qr = new QueryRunner(ds);
  try {
   result = qr.update(sql, param);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return result;
 }

  if (StringUtil.validateNull(name))
{
   message =
“对不起,姓名不可以为空,请您再也输入!<br>”;
  } else if
(StringUtil.validateNull(title)) {
   message =
“对不起,主旨不只怕为空,请你再一次输入!<br>”;
  } else {
   SimpleDateFormat sdf = new
SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
   String param[] = {
StringUtil.filterHtml(name),
StringUtil.filterHtml(request.getParameter(“email”)),
     StringUtil.filterHtml(request.getParameter(“phone”)),
StringUtil.filterHtml(title),
     request.getParameter(“content”),
sdf.format(new java.util.Date()) };
   OracleUtil db = new
OracleUtil(“java:/comp/env/jdbc/oracleds”);
   db.init();
   result = db.update(sql,
param);
   if (result == 0) {
    message =
“对不起,添加留言不成功,请你再一次输入!<B库罗德>”;
   } else {
    message =
“祝贺你,成功添加留言。<B奇骏>”;
   }
  }
  request.setAttribute(“message”,
message);
  request.getRequestDispatcher(“/addResult.jsp”).forward(request,
response);
 }
}

 public void destroy() { 
 }

 public Object query(String sql, String[] param, ResultSetHandler rsh)
{
  QueryRunner qr = new QueryRunner(ds);
  Object result = null;
  try {
   result = qr.query(sql, param, rsh);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return result;
 }
}

addResult.jsp

 public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
   ServletException {
  HttpServletRequest req =
(HttpServletRequest) request;
  HttpServletResponse res =
(HttpServletResponse) response;
  HttpSession session =
req.getSession();
  if
(session.getAttribute(“guesbook.admin.username”) == null) {
   res.sendRedirect(req.getContextPath() +
url);
  } else {
   chain.doFilter(request,
response);
  }
 }

AddMessageServlet.java

代码:

 public void init(FilterConfig config) throws ServletException {
  url = config.getInitParameter(“url”);  
 }
}

代码:

<%@ page language=”java”
contentType=”text/html; charset=UTF-8″%>
<%@ include file=”/header.jsp”
%>
<style>
* { font-family: “宋体”;font-size:
14px}
</style>
<%=request.getAttribute(“message”)%>
<a href=”<%=context
%>/addMessage.jsp”>添加新的留言</a><B宝马7系>
<a href=”<%=context
%>/servlet/getMessageServlet”>查看全体留言内容</a><B瑞鹰>
<%@ include
file=”/footer.htm”%>

————————————-Hongten————————————————

package webbook.guestbook;

GetMessageServlet.java

web.xml

import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import webbook.util.*;

代码:

代码:

public class AddMessageServlet extends HttpServlet {
 private static final long serialVersionUID = -8349454122547148005L;

package webbook.guestbook;

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
 xmlns=”http://java.sun.com/xml/ns/javaee
 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
 xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“>
 <filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>webbook.chapter15.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <filter>
  <filter-name>AuthenticationFilter</filter-name>
  <filter-class>webbook.guestbook.AuthenticationFilter</filter-class>
  <init-param>
   <param-name>url</param-name>
   <param-value>/admin/login.jsp</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping> 
 <filter-mapping>
  <filter-name>AuthenticationFilter</filter-name>
  <url-pattern>/admin/secure/*</url-pattern>
 </filter-mapping>
  
  <servlet>
    <servlet-name>AddMessageServlet</servlet-name>
   
<servlet-class>webbook.guestbook.AddMessageServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>GetMessagesServlet</servlet-name>
   
<servlet-class>webbook.guestbook.GetMessagesServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>ManageServlet</servlet-name>
   
<servlet-class>webbook.guestbook.ManageServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>AdminUserServlet</servlet-name>
   
<servlet-class>webbook.guestbook.AdminUserServlet</servlet-class>
  </servlet>

 public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String sql = “insert into guestbook
(id,name,email,phone,title,content,time)
values(gb_seq.nextval,?,?,?,?,?,?)”;
  int result = 0;
  String message = “”;
  request.setCharacterEncoding(“utf-8”);
  String name = request.getParameter(“name”);
  String title = request.getParameter(“title”);

import java.io.IOException;
import java.util.List;
import
javax.servlet.ServletException;
import javax.servlet.http.*;
import
org.apache.commons.dbutils.handlers.BeanListHandler;
import webbook.util.OracleUtil;

 
   <servlet-mapping>
    <servlet-name>AddMessageServlet</servlet-name>
    <url-pattern>/servlet/addMessage</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>GetMessagesServlet</servlet-name>
    <url-pattern>/servlet/getMessages</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AdminUserServlet</servlet-name>
    <url-pattern>/admin/user</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ManageServlet</servlet-name>
    <url-pattern>/admin/secure/manage</url-pattern>
  </servlet-mapping>
   
</web-app>

  if (StringUtil.validateNull(name)) {
   message = “对不起,姓名无法为空,请你再度输入!<br>”;
  } else if (StringUtil.validateNull(title)) {
   message = “对不起,宗旨不能为空,请你再也输入!<br>”;
  } else {
   SimpleDateFormat sdf = new
SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
   String param[] = {
StringUtil.filterHtml(name),
StringUtil.filterHtml(request.getParameter(“email”)),
     StringUtil.filterHtml(request.getParameter(“phone”)),
StringUtil.filterHtml(title),
澳门威尼斯人网址,     request.getParameter(“content”),
sdf.format(new java.util.Date()) };
   OracleUtil db = new
OracleUtil(“java:/comp/env/jdbc/oracleds”);
   db.init();
   result = db.update(sql,
param);
   if (result == 0) {
    message = “对不起,添加留言不成事,请您再也输入!<BQashqai>”;
   } else {
    message = “祝贺你,成功添加留言。<B哈弗>”;
   }
  }
  request.setAttribute(“message”, message);
  request.getRequestDispatcher(“/addResult.jsp”).forward(request,
response);
 }
}

public class GetMessagesServlet extends
HttpServlet {
 private static final long
serialVersionUID = 5964428201228635704L;

————————————-Hongten————————————————

addResult.jsp

 public void doGet(HttpServletRequest
request, HttpServletResponse response) throws ServletException,
IOException {
  String sql = “select * from guestbook
order by id desc”;
  List list = null;
  OracleUtil db = new
OracleUtil(“java:/comp/env/jdbc/oracleds”);
  db.init();
  list = (List) db.query(sql, null, new
BeanListHandler(Guestbook.class));
  HttpSession session =
request.getSession();
  session.setAttribute(“results”,
list);
  response.sendRedirect(“/guestbook6/getMessages.jsp”);
 }
}

ManagerServlet.java

代码:

getMessages.jsp

代码:

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ include file=”/header.jsp” %>
<style>
* { font-family: “宋体”;font-size: 14px}
</style>
<%=request.getAttribute(“message”)%>
<a href=”<%=context
%>/addMessage.jsp”>添加新的留言</a><B昂科拉>
<a href=”<%=context
%>/servlet/getMessages”>查看全部留言内容</a><BRAV4>
<%@ include file=”/footer.htm”%>

代码:

package webbook.guestbook;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import webbook.util.OracleUtil;

getMessages.jsp

<%@ page language=”java”
contentType=”text/html; charset=UTF-8″%>
<%@page
import=”webbook.guestbook.Guestbook”%>
<%@ include
file=”/header.jsp”%>
<style>
* {font-family: “宋体”;font-size:
14px}
</style>
<center>
 <a
href=”<%=context%>/addMessage.jsp”>添加新的留言内容</a><br>
 留言内容<br><br>
 <%
  List list =
(List)session.getAttribute(“results”); 
  if(list!=null){
  for (int i = 0; i < list.size(); i++)
{
   Guestbook gb = (Guestbook)
list.get(i);
 %>
 <table width=”600″ border=”1″
bordercolor=”000000″
  style=”table-layout: fixed; word-break:
break-all”>
  <tr>
   <td width=”100″
bordercolor=”ffffff”>
    编号:
   </td>
   <td width=”500″
bordercolor=”ffffff”><%=gb.getId()%></td>
  </tr>
  <tr>
   <td
bordercolor=”ffffff”>
    姓名:
   </td>
   <td
bordercolor=”ffffff”><%=gb.getName()%></td>
  </tr>
  <tr>
   <td
bordercolor=”ffffff”>
    电话:
   </td>
   <td
bordercolor=”ffffff”><%=StringUtil.chanageNull(gb.getPhone(),
“没填”)%></td>
  </tr>
  <tr>
   <td
bordercolor=”ffffff”>
    email:
   </td>
   <td
bordercolor=”ffffff”><%=StringUtil.chanageNull(gb.getEmail(),
“没填”)%></td>
  </tr>
  <tr>
   <td
bordercolor=”ffffff”>
    主题:
   </td>
   <td
bordercolor=”ffffff”><%=gb.getTitle()%></td>
  </tr>
  <tr>
   <td valign=”top”
bordercolor=”ffffff”>
    内容:
   </td>
   <td valign=”top”
bordercolor=”ffffff”><%=StringUtil.chanageNull(gb.getContent(),
“没填”)%></td>
  </tr>
  <tr>
   <td
bordercolor=”ffffff”>
    时间:
   </td>
   <td
bordercolor=”ffffff”><%=gb.getTime()%></td>
  </tr>
 </table>
 <br>
 <%}}%>
</center>
<%@ include
file=”/footer.htm”%>

public class ManageServlet extends HttpServlet {
 OracleUtil db = null;
 private static final long serialVersionUID = 204655027689309554L;

代码:

javaBean:

 public void edit(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String params[] = { request.getParameter(“id”) };  
  String sql = “select * from guestbook where id=?”;
  List list = (List) db.query(sql, params, new
BeanListHandler(Guestbook.class));
  request.setAttribute(“guestbook.admin.edit”, list.get(0));  
  request.getRequestDispatcher(“/admin/secure/edit.jsp”).forward(request,
response);
 }

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@page import=”webbook.guestbook.Guestbook”%>
<%@ include file=”/header.jsp”%>
<style>
* {font-family: “宋体”;font-size: 14px}
</style>
<center>
 <a
href=”<%=context%>/addMessage.jsp”>添加新的留言内容</a><br>
 留言内容<br><br>
 <%
  List list =
(List)session.getAttribute(“results”); 
  if(list!=null){
  for (int i = 0; i < list.size(); i++)
{
   Guestbook gb = (Guestbook)
list.get(i);
 %>
 <table width=”600″ border=”1″ bordercolor=”000000″
  style=”table-layout: fixed; word-break: break-all”>
  <tr>
   <td width=”100″ bordercolor=”ffffff”>
    编号:
   </td>
   <td width=”500″
bordercolor=”ffffff”><%=gb.getId()%></td>
  </tr>
  <tr>
   <td bordercolor=”ffffff”>
    姓名:
   </td>
   <td bordercolor=”ffffff”><%=gb.getName()%></td>
  </tr>
  <tr>
   <td bordercolor=”ffffff”>
    电话:
   </td>
   <td bordercolor=”ffffff”><%=StringUtil.chanageNull(gb.getPhone(),
“没填”)%></td>
  </tr>
  <tr>
   <td bordercolor=”ffffff”>
    email:
   </td>
   <td bordercolor=”ffffff”><%=StringUtil.chanageNull(gb.getEmail(),
“没填”)%></td>
  </tr>
  <tr>
   <td bordercolor=”ffffff”>
    主题:
   </td>
   <td bordercolor=”ffffff”><%=gb.getTitle()%></td>
  </tr>
  <tr>
   <td valign=”top” bordercolor=”ffffff”>
    内容:
   </td>
   <td valign=”top” bordercolor=”ffffff”><%=StringUtil.chanageNull(gb.getContent(),
“没填”)%></td>
  </tr>
  <tr>
   <td bordercolor=”ffffff”>
    时间:
   </td>
   <td bordercolor=”ffffff”><%=gb.getTime()%></td>
  </tr>
 </table>
 <br>
 <%}}%>
</center>
<%@ include file=”/footer.htm”%>

Guestbook.java

 public void update(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String params[] = { request.getParameter(“name”),
request.getParameter(“email”), request.getParameter(“phone”),
    request.getParameter(“title”), request.getParameter(“content”),
request.getParameter(“id”) };  
  int i = db.update(“update guestbook set name=?,email=?,phone=?,title=?
,content=? where id=?”, params);
  if (i == 1) {
   request.setAttribute(“guestbook.admin.update.message”,
“更新成功!”);
   Guestbook gb = new Guestbook();
   gb.setId(Integer.parseInt(request.getParameter(“id”)));
   gb.setName(request.getParameter(“name”));
   gb.setEmail(request.getParameter(“email”));
   gb.setPhone(request.getParameter(“phone”));
   gb.setTitle(request.getParameter(“title”));
   gb.setContent(request.getParameter(“content”));
   request.setAttribute(“guestbook.admin.edit”, gb);
  } else {
   request.setAttribute(“guestbook.admin.update.message”,
“更新失利!”);
  }  request.getRequestDispatcher(“/admin/secure/updateResult.jsp”).forward(request,
response);
 }

GetMessagesServlet.java

代码:

 public void delete(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String params[] = { request.getParameter(“id”) };
  int i = db.update(“delete from guestbook where id=?”, params);
  if (i == 1) {
   request.setAttribute(“guestbook.admin.delete.message”, “删除成功”);
  } else {
   request.setAttribute(“guestbook.admin.delete.message”, “删除战败”);
  }  request.getRequestDispatcher(“/admin/secure/deleteResult.jsp”).forward(request,
response);
 }

代码:

package webbook.guestbook;

 public void list(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  List list = (List) db.query(“select id,name,title from guestbook order
by id desc”, null, new BeanListHandler(
    Guestbook.class));
  request.setAttribute(“guestbook.admin.list”, list);
  request.getRequestDispatcher(“/admin/secure/list.jsp”).forward(request,
response);
 }

package webbook.guestbook;

public class Guestbook {
 private String content;
 private String email;
 private Integer id;
 private String name;
 private String phone;
 private String time;
 private String title;

 public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  db = new OracleUtil(“java:/comp/env/jdbc/oracleds”);
  db.init();
  String method = request.getParameter(“q”);
  if(method==null){
   method=”list”;
  } 
  if (method.equals(“edit”)) {
   edit(request, response);
  } else if (method.equals(“delete”)) {
   delete(request, response);
  } else if (method.equals(“update”)) {
   update(request, response);
  } else {
   list(request, response);
  }
 }

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import webbook.util.OracleUtil;

 public String getContent() {
  return content;
 }

 public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  doGet(request, response);
 }
}

public class GetMessagesServlet extends HttpServlet {
 private static final long serialVersionUID = 5964428201228635704L;

 public void setContent(String content)
{
  this.content = content;
 }

————————————-Hongten————————————————

 public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
  String sql = “select * from guestbook order by id desc”;
  List list = null;
  OracleUtil db = new
OracleUtil(“java:/comp/env/jdbc/oracleds”);
  db.init();
  list = (List) db.query(sql, null, new
BeanListHandler(Guestbook.class));
  HttpSession session =
request.getSession();
  session.setAttribute(“results”,
list);
  response.sendRedirect(“/guestbook6/getMessages.jsp”);
 }
}

 public String getEmail() {
  return email;
 }

updateResult.jsp

Guestbook.java

 public void setEmail(String email)
{
  this.email = email;
 }

代码:

代码:

 public Integer getId() {
  return id;
 }

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@taglib
uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<%@ include file=”/header.jsp” %>
<style>* { font-family: “宋体”;font-size: 14px}</style>
<center>
 <c:out
value=”${requestScope[‘guestbook.admin.update.message’]}”/><br>
 <c:set var=”gb” value=”${requestScope[‘guestbook.admin.edit’]}”
/> 
 <table width=”600″ border=”1″ bordercolor=”000000″
style=”table-layout: fixed; word-break: break-all”>
  <tr><td width=”100″
bordercolor=”ffffff”>编号:</td>
   <td width=”500″ bordercolor=”ffffff”><c:out
value=”${gb.id}”/></td>
  </tr>
  <tr><td bordercolor=”ffffff”>姓名:</td>
   <td bordercolor=”ffffff”><c:out
value=”${gb.name}”/></td>
  </tr>
  <tr><td bordercolor=”ffffff”>电话:</td>
   <td bordercolor=”ffffff”><c:out value=”${gb.phone}”
default=”没填” /></td> 
  </tr>
  <tr><td bordercolor=”ffffff”>email: </td>
   <td bordercolor=”ffffff”><c:out value=”${gb.email}”
default=”没填” /></td>
  </tr>
  <tr><td bordercolor=”ffffff”>主题:
   </td><td bordercolor=”ffffff”><c:out
value=”${gb.title}” default=”没填” /></td>
  </tr>
  <tr><td valign=”top”
bordercolor=”ffffff”>内容: </td>
   <td valign=”top” bordercolor=”ffffff”><c:out
value=”${gb.content}” default=”没填” escapeXml=”false”/></td>
  </tr>
  <tr><td bordercolor=”ffffff”>时间:</td>
   <td bordercolor=”ffffff”><c:out
value=”${gb.time}”/></td>
  </tr>
 </table>
</center>
<br><a
href=”${ctx}/admin/secure/manage?q=list”>再次回到管理首页</a><br>
<%@ include file=”/footer.htm”%>

package webbook.guestbook;

 public void setId(Integer id) {
  this.id = id;
 }

————————————-Hongten————————————————

public class Guestbook {
 private String content;
 private String email;
 private Integer id;
 private String name;
 private String phone;
 private String time;
 private String title;

 public String getName() {
  return name;
 }

deleteResult.jsp

 public String getContent() {
  return content;
 }

 public void setName(String name)
{
  this.name = name;
 }

代码:

 public void setContent(String content) {
  this.content = content;
 }

 public String getPhone() {
  return phone;
 }

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@taglib
uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
<%@ include file=”/header.jsp” %>
<style>* { font-family: “宋体”;font-size: 14px}</style>
<center>
 <c:out
value=”${requestScope[‘guestbook.admin.delete.message’]}”/><br>
 <a
href=”${ctx}/admin/secure/manage?q=list”>再次来到管理首页</a><br>
</center>
<%@ include file=”/footer.htm”%>

 public String getEmail() {
  return email;
 }

 public void setPhone(String phone)
{
  this.phone = phone;
 }

list.jsp

 public void setEmail(String email) {
  this.email = email;
 }

 public String getTime() {
  return time;
 }

代码;

 public Integer getId() {
  return id;
 }

 public void setTime(String time)
{
  this.time = time;
 }

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core
prefix=”c”%>
<%@ include file=”/header.jsp”%>
<style>
* {font-family: “宋体”;font-size: 14px}
</style>
<center>
 <a
href=”${ctx}/admin/user?q=logout”>退出登录</a><br><br> 
 全体留言内容<br><br> 
 <c:forEach items=”${requestScope[‘guestbook.admin.list’]}” 
var=”gb”> 
 <table width=”600″ border=”1″ bordercolor=”000000″
  style=”table-layout: fixed; word-break: break-all”>
  <tr><td width=”100″ bordercolor=”ffffff”>留言编号:
   </td><td width=”500″
bordercolor=”ffffff”>${gb.id}</td>
  </tr>
  <tr><td bordercolor=”ffffff”>作者:</td>
   <td bordercolor=”ffffff”><c:out value=”${gb.name}”
default=”没填” /></td>
  </tr>
  <tr><td bordercolor=”ffffff”>主题:
   </td><td bordercolor=”ffffff”><c:out
value=”${gb.title}” default=”没填” /></td>
  </tr>
  <tr><td width=”100″
bordercolor=”ffffff”>操作选项:</td>
   <td width=”500″ bordercolor=”ffffff”>
   <a
href=”${ctx}/admin/secure/manage?q=edit&id=${gb.id}”>编辑</a>
   <a
href=”${ctx}/admin/secure/manage?q=delete&id=${gb.id}”>删除</a></td>
  </tr>  
 </table>
 <br>
 </c:forEach> 
</center>
<%@ include file=”/footer.htm”%>

 public void setId(Integer id) {
  this.id = id;
 }

 public String getTitle() {
  return title;
 }

————————————-Hongten————————————————

 public String getName() {
  return name;
 }

 public void setTitle(String title)
{
  this.title = title;
 }
}

edit.jsp

 public void setName(String name) {
  this.name = name;
 }

OracleUtil.java

代码:

 public String getPhone() {
  return phone;
 }

代码:

<%@ page language=”java” contentType=”text/html;
charset=UTF-8″%>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core
prefix=”c”%>
<%@ include file=”/header.jsp”%>
<style>* { font-family: “宋体”; font-size: 14px }</style>
<script type=”text/javascript”
src=”${ctx}/fckeditor/fckeditor.js”></script>
<p align=”center”>修改留言</p>
<p align=”center”>
 <br><a
href=”${ctx}/admin/secure/manage?q=list”>再次来到管理首页</a>|<a
href=”${ctx}/admin/user?q=logout”>退出登录</a><br></p>
 <c:set var=”gb” value=”${requestScope[‘guestbook.admin.edit’]}”
/> 
<form id=”form1″ name=”form1″
method=”post” action=”${ctx}/admin/secure/manage”>
 <input type=”hidden” name=”id” value=”${gb.id}”>
 <input type=”hidden” name=”q” value=”update”>
 <table width=”650″ height=”400″ border=”0″ align=”center”>
  <tr><td width=”150″>姓名:</td> <td
width=”500″>
   <input name=”name” type=”text” id=”name” size=”40″ 
value=”${gb.name}” >
   </td></tr>
  <tr><td>E-Mail:</td><td>
   <input name=”email” type=”text” id=”email” size=”40″
value=”${gb.email}” ></td></tr>
  <tr><td>电话:</td><td>
   <input name=”phone” type=”text” id=”phone” size=”40″ 
value=”${gb.phone}” >
  </td></tr>
  <tr><td>主题:</td><td>
  <input name=”title” type=”text” id=”title” size=”80″ 
value=”${gb.title}” >
  </td></tr>  
  <script type=”text/javascript”>
   window.onload = function(){ 
    var oFCKeditor = new FCKeditor( ‘content’ ) ;
    oFCKeditor.BasePath = ‘${ctx}/fckeditor/’ ;
    oFCKeditor.ToolbarSet = ‘Basic’;
       oFCKeditor.Height = 300 ;
    oFCKeditor.ReplaceTextarea() ;
   }
  </script>
  <tr><td valign=”top”>内容:</td><td>
   <textarea name=”content” rows=”10″
cols=”80″>${gb.content}</textarea> 
   </td></tr>
  <tr><td></td><td>        
   <input type=”submit” name=”Submit” value=”提交” />
   <input type=”reset” name=”Reset” value=”重置” />
  </td></tr>
 </table>
</form>
<%@ include file=”/footer.htm”%>

 public void setPhone(String phone) {
  this.phone = phone;
 }

package webbook.util;

————————————-Hongten————————————————

 public String getTime() {
  return time;
 }

import java.sql.SQLException;
import javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.naming.NamingException;
import javax.sql.DataSource;
import
org.apache.commons.dbutils.QueryRunner;
import
org.apache.commons.dbutils.ResultSetHandler;

    感谢:v512工作室-刘伟

 public void setTime(String time) {
  this.time = time;
 }

public class OracleUtil {
 private String dataSourceName;
 private DataSource ds;

————————————-Hongten————————————————

 public String getTitle() {
  return title;
 }

 public OracleUtil(String dataSourceName)
{
  this.dataSourceName =
dataSourceName;  
 }

 public void setTitle(String title) {
  this.title = title;
 }
}

 public OracleUtil() {  
 }

 public void setDataSourceName(String
dataSourceName) {
  this.dataSourceName =
dataSourceName;
 }

 public void init() {
  Context initContext;
  try {
   initContext = new
InitialContext();
   ds = (DataSource)
initContext.lookup(dataSourceName);
  } catch (NamingException e) {
   e.printStackTrace();
  }
 }

//跟新数据库中数据,蕴含插入,删除,修改

 public int update(String sql, String[]
param) {
  int result = 0;
  QueryRunner qr = new
QueryRunner(ds);
  try {
   result = qr.update(sql,
param);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return result;
 }

//query(String sql,
String[] param, ResultSetHandler
rsh)方法是概念数据库的询问

参数:sql:查询的sql语句,

   params:传递的参数

   rsh:BeanListHandler的对象

 public Object query(String sql,
String[] param, ResultSetHandler rsh) {
  QueryRunner qr = new
QueryRunner(ds);
  Object result = null;
  try {
   result = qr.query(sql, param,
rsh);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return result;
 }
}

相关文章