導航:首頁 > 源碼編譯 > servlet3源碼

servlet3源碼

發布時間:2025-06-19 15:04:19

Ⅰ idea 運行JSP後顯示源代碼是什麼情況

這種情況,是jsp的內容被當做文本直接顯示到了頁面上,一般在使用springMVC時可能出現這樣的問題,猜測可能使用了springMVC。

具體解決方案:

查找web.xml文件,並找到springMVC的相關配置

<servlet-mapping>

<servlet-name>springMVC</servlet-name>

<url-pattern>/*</url-pattern>

</servlet-mapping>

(1)servlet3源碼擴展閱讀:

快捷鍵:

1、寫代碼時用Alt-Insert(Code|Generate…)可以創建類裡面任何欄位的getter與setter方法。

2、按Ctrl-N再鍵入類的名字可以快速地在編輯器里打開任何一個類。從顯示出來的下拉列表裡選擇類。同樣的方法你可以通過使用Ctrl-Shift-N打開工程中的非java文件。

3、Ctrl-D復制當前行,Ctrl-Y刪除當前行。

4、Ctrl-Shift-U,Ctrl+Shift+Alt+U顯示類繼承結構圖,再按Alt-M。

5、Alt-F1 回到當前文件所在的目錄結構。

6、Ctrl-Shift-V 粘貼最近復制過的一些信息。

7、Ctrl-Shift-F7 高亮顯示所有該文本,按Esc高亮消失。

8、Ctrl-E 最近打開的文件。

9、Ctrl-P 方法參數提示。

Ⅱ 嚴重: Servlet.service() for servlet jsp threw exception

出現此錯誤一般都是在jsp中使用了輸出流(如輸出圖片驗證碼,文件下載等),
沒有妥善處理好的原因。

具體的原因就是
在tomcat中jsp編譯成servlet之後在函數_jspService(HttpServletRequest request, HttpServletResponse response)的最後
有一段這樣的代碼
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
這里是在釋放在jsp中使用的對象,會調用response.getWriter(),因為這個方法是和
response.getOutputStream()相沖突的!所以會出現以上這個異常。

然後當然是要提出解決的辦法,其實挺簡單的(並不是和某些朋友說的那樣--
將jsp內的所有空格和回車符號所有都刪除掉),

在使用完輸出流以後調用以下兩行代碼即可:
out.clear();
out = pageContext.pushBody();

最後這里是一個輸出彩色驗證碼例子(這樣的例子幾乎隨處可見)
imag.jsp

<%@ page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%@ page import="java.io.OutputStream" %>
<%!
Color getRandColor(int fc,int bc){
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
try{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
OutputStream os=response.getOutputStream();
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);

g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String sRand="";
for (int j=0;j<4;j++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*j+6,16);
}
session.setAttribute("rand",sRand);
g.dispose();

ImageIO.write(image, "JPEG",os);
os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
catch(IllegalStateException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}%>

如有不足之處,歡迎斧正!

2

getOutputStream() has already been called for this response問題的解決
在jsp向頁面輸出圖片的時候,使用response.getOutputStream()會有這樣的提示:java.lang.IllegalStateException:getOutputStream() has already been called for this response,會拋出Exception

原因一:
JSP默認的輸出流為PrintWriter ,即<% %>以外的東西所默認的輸出方式,如果你嘗試在JSP中使用ServletOutputStream就會引起錯誤.要嘛直接改用Servlet輸出(復寫service方法),要嘛刪除除%><%中的任何東西
(包括HTML標簽,空格,回車等東西)應該就可以。
對於這樣的情況應該這樣來解決,刪除%><%之間的所有內容包括空格和換行符,最後也要消除空格和換行符,
最好再加上一句response.reset()。
原因二:

在J2EE的API參考里有這么個:

ServletResponse的getWriter()方法里會拋出這個異常,

IllegalStateException - if the getOutputStream method has already been called
for this response object

而它的getOutputStream()方法里會拋出這個異常.

IllegalStateException - if the getOutputStream method has already been called for this response object

並且兩者的函數申明裡都有這么樣的一句
Either this method or getOutputStream() may be called to write the body, not both.
Either this method or getWriter() may be called to write the body, not both.

以上說明也解釋了為什麼在往頁面中寫入圖片的時候要使用如下循環格式
OutputStream output=response.getOutputStream();
while((len=in.read(b)) >0)
{
output.write(b,0,len);

}
output.flush();
而不是把response.getOutputStream().write()放到循環體內

在頁面中直接寫:
<body bgcolor="#ffffff">
<h1>
<%
response.getOutputStream();
%>
</h1>
</body>
將會出現錯誤消息如下:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.catalina.connector.Response.getWriter(Response.java:604)
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)

閱讀全文

與servlet3源碼相關的資料

熱點內容
程序的執行編譯和翻譯 瀏覽:170
發圖片文件夾的格式 瀏覽:94
將數據加密儲存到資料庫 瀏覽:972
集權伺服器是什麼 瀏覽:392
故事存檔在哪個文件夾 瀏覽:132
程序員欠債29萬 瀏覽:244
錘子手機拍攝的照片在哪個文件夾 瀏覽:96
ca指標源碼大全 瀏覽:241
為什麼國內手機攝像頭不做演算法 瀏覽:182
蘋果手機的app怎麼不顯示 瀏覽:129
不用的文件夾可以做什麼 瀏覽:22
win10c語言編程軟體 瀏覽:334
刪掉的平安app怎麼找回來 瀏覽:842
javastring相加 瀏覽:647
單片機設置斷點 瀏覽:522
wfiif怎麼加密 瀏覽:413
蘋果手機加密組件 瀏覽:814
線切割單片機操作指令 瀏覽:942
歐姆龍plc編程視頻 瀏覽:954
rsa加密演算法實驗 瀏覽:611