jsp 用application對象製作留言板

來源:互聯網
上載者:User

標籤:context   tle   action   html   cat   encoding   log   輸入   java   

 1 <%@ page contentType="text/html; charset=gb2312"%> 2 <html> 3 <body> 4     <form action="show.jsp" method="post"> 5         輸入名字:<input type="text" name="name"><br> 留言標題:<input 6             type="text" name="title"><br> 留言:<br> 7         <textarea rows="10" cols="30" name="text" wrap="physical"></textarea> 8         <br> <input type="submit" value="提交"><br> 9     </form>10     <form action="chuli.jsp" method="get">11         <input type="submit" name="look" value="查看留言板"><br>12     </form>13 14 15 </body>16 </html>


<%@ page contentType="text/html; charset=gb2312"%><%@page import="java.util.Vector"%><%@ page import="java.util. *"%><%    request.setCharacterEncoding("gb2312");    String name = request.getParameter("name");    String title = request.getParameter("title");    String text = request.getParameter("text");    if (name == null || name.length() == 0)        name = "";    if (title == null || title.length() == 0)        title = "";    if (text == null || text.length() == 0)        text = "";    String s = name + "#" + title + "#" + text;    add(s);%><%!Vector vector = new Vector();    ServletContext application;    //有些伺服器不直接支援application對象,    //必須用ServletContext 類類聲明這個對象,    //再使用getServletContext方法    //對application對象初始化    int i = 0;    synchronized void add(String s) {        application = getServletContext();        i++;        vector.add("NO." + i + "," + s);        application.setAttribute("mess", vector);//如果添加的2個對象的關鍵字相同,則先前的關鍵字被清除,mess叫索引關鍵字    }%><a href="chuli.jsp">返回留言介面</a>

 

 

<%@ page contentType="text/html; charset=gb2312"%><%@ page import="java.util.Vector"%><%!public String handString(String s) {        try {            byte b[] = s.getBytes("gb2312");            s = new String(b);        } catch (Exception exp) {        }        return s;    }%><%    Vector vector = (Vector) application.getAttribute("mess");    //因為任何對象都可以添加到application中,取回對象時,    //所以要強制轉回原來的類型。    for (int i = 0; i < vector.size(); i++) {        //vector的長度就是vector.size();        String message = (String) vector.elementAt(i);        //Vector就是一個放資料的地方啊。elementat(i)就是取出資料的作用        String str[] = message.split("#");        out.print("留言人:" + handString(str[0]) + ";");        out.print("標題:" + handString(str[1]) + "<br>");        out.print("留言內容:" + "<br>" + handString(str[2]));    }%><a href="input.jsp">返回主介面</a>

 

jsp 用application對象製作留言板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.