在IDEA的項(xiàng)目中,通過瀏覽器訪問Servlet(localhost:8080/Identifier)可以正常訪問,
而localhost:8080/login.html、http://localhost:8080/index.jsp
或者訪問其他并不存在的文件的情況下都顯示一個(gè)空白頁(yè)。
在新建一個(gè)項(xiàng)目之后以同樣的方式訪問index.jsp卻可以訪問到。
login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" method="post">
用戶名:<input type="text" name="userName"><br>
密碼: <input type="password" name="pwd"><br>
驗(yàn)證碼:<input type="text" name="code"><img src="/Identifier" alt="驗(yàn)證碼"><br>
<input type="submit" value="登陸">
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.lonelycorn.ServletDemo</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servletDemo2</servlet-name>
<servlet-class>com.lonelycorn.ServletDemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo2</servlet-name>
<url-pattern>/demo2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servletDemo3</servlet-name>
<servlet-class>com.lonelycorn.ServletDemo3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Servlet
package com.lonelycorn;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet(urlPatterns = "/Identifier")
public class Identifier extends HttpServlet {
int width = 150;
int height =50;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//創(chuàng)建一個(gè)畫筆
Graphics g = bufferedImage .getGraphics();
//給圖片添加背景色
g.setColor(Color.yellow);
g.fillRect(1,1,width-2,height-2);
//給邊框一個(gè)顏色
g.setColor(Color.BLUE);
g.drawRect(0,0,width-1,height-1 );
//設(shè)置文本樣式
g.setColor(Color.GREEN);
g.setFont(new Font("宋體",Font.BOLD|Font.ITALIC,15));
//給圖片添加文本
Random r = new Random();
int position= 20;
for (int i = 0; i < 4; i++) {
g.drawString(r.nextInt(10)+"",position+=20,30);
}
//添加干擾線
for (int i = 0; i < 9; i++) {
g.setColor(Color.RED);
g.drawLine(r.nextInt(width),r.nextInt(height),r.nextInt(width),r.nextInt(height));
}
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
}
新建的項(xiàng)目,有HttpServlet嗎?還是只有jsp等靜態(tài)文件。
你在web.xml中有一段這么配置
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
這段會(huì)把所有的url(/*)都交給servletDemo3來處理,但你的ServletDemo3似乎沒有做靜態(tài)文件的處理。
所以之前的這種web項(xiàng)目,都會(huì)用.do作為后綴(現(xiàn)在已經(jīng)不這么干了)
還有另一個(gè)原因可能是因?yàn)闆]有開啟靜態(tài)資源訪問,這個(gè)一樣是需要配置的。自己上網(wǎng)查找下配置。
servlet的項(xiàng)目很久沒碰過了...
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。