下一个servlet代码:
import java.io.IOException;
//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "HomeServlet", urlPatterns = {"/home"})
public class HomeServlet extends HttpServlet {
public HomeServlet() {
super();
}
/*protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Forward to /WEB-INF/views/homeView.jsp
// (Users can not access directly into JSP pages placed in WEB-INF)
request.getRequestDispatcher("/Career/WebContent/WEB-INF/views/index.jsp").forward(request, response);
// dispatcher.forward(request, response);
}*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
下一个索引jsp页面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<link rel="stylesheet" href="${contextPath}/css/styles.css"
type="text/css" />
</head>
<body>
<div id="wrapper">
<div class="header" id="header"></div>
<div class="navbar">
<ul>
<li><a href="/index.jsp">Home</a></li>
<li><a href="#">Profile</a></li>
<li><a href="job_offer.html" target="_blank">Offer a job</a></li>
</ul>
<a href="index.html" target="_blank"><img
src="${contextPath}/images/meterbrain.gif"
style="float: right; width: 12%;" /></a>
</div>
<div class="content">
<div class="rightblock">
<div class="headmenu">Prolog System</div>
<div class="bodymenu">
<a href="#"><img src="${contextPath}/images/meterbrain.gif" /></a>
</div>
</div>
<div class="leftblock">
<div class="headmenu">Advertisements</div>
<div class="bodymenu" id="advertise1">
<a href="#"><img src="${contextPath}/images/advertisement.jpg" /></a>
</div>
<div class="bodymenu" id="advertise1">
<a href="#"><img src="${contextPath}/images/advertisement.jpg" /></a>
</div>
<div class="bodymenu" id="advertise1">
<a href="#"><img src="${contextPath}/images/advertisement.jpg" /></a>
</div>
</div>
下一个web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Career</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
和另一个问题 如果我运行此页面为什么我无法将css和图像导入到jsp中,并将这些文件添加到eclipse文件夹中。
上次更新图片项目文件夹:
仍无法在jsp文件中导入css和图像。 和请求另一个jsp页面相同的问题。 任何帮助。
答案 0 :(得分:1)
您需要将从WAR文件开始的相对路径传递给getRequestDispatcher()方法。
request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);
从屏幕截图中,我看不到您保存css和图像文件的位置。在WebContent下创建文件夹'css'和'images'。在JSP中包含它们。
<link rel="stylesheet" href="${contextPath}/css/styles.css" type="text/css" />
<img src="${contextPath}/images/meterbrain.gif" />