Archive for JSP

ENCTYPE="multipart/form-data"

用于表单里有檔案或图片上传

<form name="userInfo" method="post" action="first_submit.jsp"   ENCTYPE="multipart/form-data">
表单标签中设置enctype="multipart/form-data"来确保匿名上载文件的正确编码
如下:
<tr>
<td height="30″ align="right">上传图片:</td>
<td><INPUT TYPE="FILE" NAME="uploadfile" SIZE="34″   onChange="checkimage()"></td>
</tr>
就得加ENCTYPE="multipart/form-data"。

表 单中enctype="multipart/form-data"的意思,是设置表单的MIME编码默认情况,这个编码格式是application /x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下 面的操作.
enctype=\"multipart/form-data\"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,
取表单值加入数据库时,用到下面的:
SmartUpload su = new SmartUpload();//新建一个SmartUpload对象
su.getRequest().getParameterValues();取数组值
su.getRequest().getParameter( );取单个参数单个值

發表迴響

getServletContext()和getServletConfig()的意思

getServletConfig()在servlet初始化时,容器传递进来一个ServletConfig对象并保存在servlet实例中,该对象允许访问两项内容:初始化参数和 ServletContext对象,前者通常由容器在文件中指定,允许在运行时向sevrlet传递有关调度信息,比如说getServletConfig().getInitParameter(“debug")后者为servlet提供有关容器的信息。此方法可以让servlet在任何时候获得该对象及配置信息。

 

getServletContext()
一个servlet可以使用getServletContext()方法得到web应用的servletContext
即而使用getServletContext的一些方法来获得一些值
比如说getServletContext().getRealPath(“/")来获得系统绝对路径
getServletContext().getResource(“WEB-INF/config.xml")来获得xml文件的内容
文章出處:
http://www.blogjava.net/shichengjun1984/articles/159935.html

發表迴響

getOutputStream() has already been called for this response

朋友在處理Jsp時, 希望能夠將產生好的excel檔案能夠下載到Client 端
但要將產生好的file利用 SmartUpload 下載時, 發生以下問題
getOutputStream() has already been called for this response
後來找到了解決方法如下:
<範例>

@page contentType="text/html; charset=big5″
@page session="true" errorPage="error.jsp"
@page import="com.jspsmart.upload.*"
String ret = request.getParameter(“ret");
if (ret != null) {
ret = java.net.URLDecoder.decode(ret);
ret = new String(ret.getBytes(“8859_1″),"Big5″);
}
else ret = "";
String file = request.getParameter(“file");
if (file != null) {
file = java.net.URLDecoder.decode(file);
file = new String(file.getBytes(“8859_1″),"Big5″);
} else file = "";
// 新建一個SmartUpload對象
SmartUpload su1 = new SmartUpload();
// 初始化
su1.initialize(pageContext);
// 設定contentDisposition為null以禁止瀏覽器自動打開文件,
//保證點擊鏈接後是下載文件。若不設定,則下載的文件擴展名為
//doc時,瀏覽器將自動用word打開它。擴展名為pdf時,
//瀏覽器將用acrobat打開。
su1.setContentDisposition(null);
// 下載文件
su1.downloadFile(file);

問題原因:Tomcat首先執行.jsp, Tomcat準備好session, out等object。 而在< % … % >段中,HttpServerletResponse的getOutputStream()方法已被呼叫。但在JSP規中定義此方法只能被使用一 次,這樣在產生out時會在使用一次, 因此會出錯。
網路上建議方法: 不要使用Jsp, 改使用Servlet就不會有此問題
後來有人回應在最後加入兩行
out.clear();
out = pageContext.pushBody();

果然解決了這個問題!!

文章出處:

http://www.blogjava.net/honghongw/articles/197893.html

發表迴響

顯示目前session中所有屬性值: session.getAttributeNames()

<%@ page language="java" contentType="text/html; charset=UTF-8″ pageEncoding="UTF-8″
%><%@ page import="java.util.*%>

<%
Enumeration enums = session.getAttributeNames();
%><table><tr><td>name</td><td>value</td></tr><%
while( enums.hasMoreElements() ){
Object name = enums.nextElement();
%><tr><td><%=name%></td><td><%=session.getAttribute((String)name)%></td></tr><%
}
%></table><%
%>

參考頁面:

http://topic.csdn.net/t/20050616/11/4086312.html

http://www.coderanch.com/t/292546/JSP/java/session-getAttributeNames

發表迴響

Follow

Get every new post delivered to your Inbox.