zoukankan      html  css  js  c++  java
  • Java Web 网络留言板6 MVC模式

    两种转向的区别:

      forward()

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

      sendRedirect()

      response.sendRedirect("/guestbook6/getMessages.jsp");

     

    区别:

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

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

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

    4、sendRedirect()方法可以转向到web应用之外的页面或者网站,

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

      而forward()是不可以的; 

    addMessage.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="<%=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"%>

    OracleUtil.java

    代码:

    package webbook.util;

    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;

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

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

     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;
     }

     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;
     }
    }

    AddMessageServlet.java

    代码:

    package webbook.guestbook;

    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;

     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");

      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 = "对不起,添加留言不成功,请您重新输入!<BR>";
       } else {
        message = "祝贺您,成功添加留言。<BR>";
       }
      }
      request.setAttribute("message", message);
      request.getRequestDispatcher("/addResult.jsp").forward(request, response);
     }
    }

    addResult.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><BR>
    <a href="<%=context %>/servlet/getMessages">查看所有留言内容</a><BR>
    <%@ include file="/footer.htm"%>

    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"%>

    GetMessagesServlet.java

    代码:

    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;

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

     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");
     }
    }

    Guestbook.java

    代码:

    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 String getContent() {
      return content;
     }

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

     public String getEmail() {
      return email;
     }

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

     public Integer getId() {
      return id;
     }

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

     public String getName() {
      return name;
     }

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

     public String getPhone() {
      return phone;
     }

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

     public String getTime() {
      return time;
     }

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

     public String getTitle() {
      return title;
     }

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

  • 相关阅读:
    一份面试题+整理的答案
    java获取本机IPV4地址,非127.0.0.1
    List去重
    禁止iframe页面时产生历史记录
    js打开新窗口并且不被拦截
    ifream 跨域实现高度自适应
    css版tooltip
    jQuery遮罩插件 jQuery.blockUI.js
    SpringMVC 传递相同名称的参数
    SpringMVC接收List型参数
  • 原文地址:https://www.cnblogs.com/hongten/p/2115195.html
Copyright © 2011-2022 走看看