使用servlet显示jsp时,如何使css应用于jsp?
How to make css apply to jsp when using servlet to show jsp?
使用 Eclipse - 动态 Web 项目。
我基本上是通过 servlet
中的这段代码显示页面 "Home.jsp"
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.getRequestDispatcher("/WEB-INF/Home.jsp").forward(request, response);
}
但是样式并没有像通过 url 直接显示页面(或者直接在文件系统中直接打开 html 文件)那样应用样式。有什么方法可以在 doGet 方法(或其他地方)中应用它们吗?
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>System</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<button type="button" class="homebutt">Concerts</button>
<button type="button" class="loginbutt">Login</button>
<button type="button" class="registrationbutt">Registration</button>
<hr>
<div>
<select id="artistcombo">
<option value="">Artist:</option>
</select>
<select id="countrycombo">
<option value="">Country:</option>
</select>
<select id="genrecombo">
<option value="">Genre:</option>
</select>
</div>
<hr>
</body>
</html>
css:
button
{
font-family: Helvetica, sans-serif;
}
button.homebutt
{
font-size: 30px;
text-align: left;
background: none;
border: none;
color: indigo;
}
button.loginbutt
{
color: black;
background: none;
border: none;
font-size: 24px;
float: right;
text-align: right;
}
button.registrationbutt
{
color: black;
background: none;
border: none;
font-size: 24px;
float: right;
text-align: right;
}
项目树:
最终用户无法直接访问 WEB-INF
目录中的所有文件。我的建议是
- 将您的 css 文件移动到
WEB-INF
之外但处于同一级别的目录,例如resources
- 将 css 文件在 jsp 中的导入更改为
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/style.css/>"
使用 Eclipse - 动态 Web 项目。 我基本上是通过 servlet
中的这段代码显示页面 "Home.jsp"protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.getRequestDispatcher("/WEB-INF/Home.jsp").forward(request, response);
}
但是样式并没有像通过 url 直接显示页面(或者直接在文件系统中直接打开 html 文件)那样应用样式。有什么方法可以在 doGet 方法(或其他地方)中应用它们吗?
jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>System</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<button type="button" class="homebutt">Concerts</button>
<button type="button" class="loginbutt">Login</button>
<button type="button" class="registrationbutt">Registration</button>
<hr>
<div>
<select id="artistcombo">
<option value="">Artist:</option>
</select>
<select id="countrycombo">
<option value="">Country:</option>
</select>
<select id="genrecombo">
<option value="">Genre:</option>
</select>
</div>
<hr>
</body>
</html>
css:
button
{
font-family: Helvetica, sans-serif;
}
button.homebutt
{
font-size: 30px;
text-align: left;
background: none;
border: none;
color: indigo;
}
button.loginbutt
{
color: black;
background: none;
border: none;
font-size: 24px;
float: right;
text-align: right;
}
button.registrationbutt
{
color: black;
background: none;
border: none;
font-size: 24px;
float: right;
text-align: right;
}
项目树:
最终用户无法直接访问 WEB-INF
目录中的所有文件。我的建议是
- 将您的 css 文件移动到
WEB-INF
之外但处于同一级别的目录,例如resources
- 将 css 文件在 jsp 中的导入更改为
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/style.css/>"