鍍金池/ 問答/Java  Linux/ IDEA訪問index時(shí)出現(xiàn)空白頁(yè)

IDEA訪問index時(shí)出現(xiàn)空白頁(yè)

在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)目索引頁(yè)面,直接訪問localhost:8080就行了,會(huì)自動(dòng)訪問index.jsp的內(nèi)容,而localhost:8080/index.jsp是另外一個(gè)請(qǐng)求了,這個(gè)請(qǐng)求會(huì)被你寫的servlet攔截掉,html也是一樣的。
解決方案,要么配置靜態(tài)資源,要么在servlet里面自己寫代碼不攔截。

2017年10月29日 04:10
編輯回答
茍活

新建的項(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)目很久沒碰過了...

2017年9月18日 19:26