正在回答
3回答
WEB-INF目录下的jsp页面不能直接用路径进行跳转,需要在servlet中进行跳转,譬如:我在img src下引用一下jump(这个jump是我自己写的一个servlet,注意还要在web.xml中注册一下)如下图所示
xml

JumpServlet,在这里我设置了WEB-INF下的code.jsp的路径。

然后在index.jsp页面中img src下的路径是这样的。

我的目录结构与同学的不是完全一致,主要提供的是一个思路,同学可以按照上述思路修改,修改之后的效果如下图:

如果我的建议解决了你的问题,请采纳,祝学习愉快~
慕容5097141
2018-07-11 19:34:19
basePath.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() ;
request.setAttribute("basePath",basePath);
%>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录页面</title>
<style type="text/css">
.code
{
/*background:url("");*/
font-family:Arial;
font-style:italic;
color:blue;
font-size:20px;
border:0;
padding:2px 3px;
letter-spacing:3px;
font-weight:bolder;
float:left;
cursor:pointer;
width:40px;
height:20px;
line-height:20px;
text-align:center;
vertical-align:middle;
}
a
{
text-decoration:none;
font-size:12px;
color:#288bc4;
}
a:hover
{
text-decoration:underline;
}
</style>
<script>
var msg = "${msg}";
if(msg == 1){
alert("请按格式正确输入账号密码");
}
</script>
</head>
<body>
${basePath}
<p>1<%=request.getContextPath()%>1</p>
<center>
<h1>用户登录</h1>
<form action="<%=basePath%>/login" method="post" onsubmit="return validateCode()">
<table width="300px" cellspacing="0px" cellpadding="0px" border="1px">
<tr>
<td>用户名</td>
<td colspan="2"><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合"></td>
</tr>
<tr>
<td>密 码</td>
<td colspan="2"><input type="password" name="password" placeholder="长度为5-12位字母数字或下划线组合" ></td>
</tr>
<tr>
<td>验证码</td>
<td style="border-right-style:none;">
<input type="text" name="checkCode" placeholder="请输入验证码" id="inputCode">
</td>
<td style="border-left-style:none;"><div class="code" id="checkCode" ><img src="${basePath}/CourseSystem/WEB-INF/views/verficationcode/code.jsp"></div></td>
</tr>
<tr>
<td colspan="3" style="text-align:center">
<input type="submit" value="登录">
<input type="reset" value="取消">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
code.jsp
<%@ page import="com.imooc.Util.VerificationCode" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//清空浏览器缓存
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
System.out.println("code");
//调用生成验证码
String code = VerificationCode.drawImage(response);
session.setAttribute("code",code);
//清除异常
out.clear();
out = pageContext.pushBody();
%>
chrismorgen
2018-07-11 18:37:26
图片地址显示正确,但是还有报404的错误,没明白同学是什么意思,是验证码正常显示?但是在控制台上出现404的错误码?
Java Web基础入门2018版
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星