標籤: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對象製作留言板