Web开发
见倒数第五行,为什么查找不到路径?<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="RegAction" method="post" enctype="multipart/form-data">
<h1>注册页面</h1>
<h1>21 人工智能 徐家豪</h1>
<hr width="100%" size="5" color="black" >
<p style="color: #ff0000">${username}</p>
<p>${password}</p>
<p>${sex}</p>
<p>${major}</p>
<p>${like}</p>
<img src="girl.jpg" width="200px"/><br>
用户名:<input type="text" name="username"/>
<br><br>
密 码:<input type="password" name="password"/>
<br><br>
性 别:
<input type="radio" name="sex" value="男" checked/>男
<input type="radio" name="sex" value="女"/>女
<br><br>
兴 趣:<!-- 复选框 -->
<input type="checkbox" name="like" value="唱"/>唱
<input type="checkbox" name="like" value="跳"/>跳
<input type="checkbox" name="like" value="rap"/>rap
<input type="checkbox" name="like" value="篮球"/>篮球
<br><br>
学 历:<!-- 下拉列表 -->
<select name="major">
<option value="信息管理">信息管理</option>
<option value="软件工程">软件工程</option>
<option selected value="人工智能">人工智能</option>
</select>
<br><br>
简 介:<textarea rows="5" cols="20" name="introduction"></textarea>
<br><br>
文件:<input type="file" name="myFile"/>
<br><br>
<input type="submit" value="注册"/><!-- 不能有name,不然也会提交给服务器 -->
<input type="reset" value="重置"/>
</form>
</body>
</html>
package xjh.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.xml.soap.SOAPElementFactory;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.UUID;
@WebServlet("/RegAction")
@MultipartConfig(location = "D:/jerbrain/img", maxFileSize = 1024 * 1024 * 5)
public class RegActionServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
System.out.println("id:" + req.getRemoteAddr());
System.out.println(new Date());
String username = req.getParameter("username");
String password = req.getParameter("password");
String sex = req.getParameter("sex");
String major = req.getParameter("major");
// String like=req.getParameter("like");
String[] like = req.getParameterValues("like");
String s = "";
if (like == null) {
s = "无";
} else {
for (String L : like) {
s += L;
s += " ";
}
}
System.out.println("username:" + username);
System.out.println("password:" + password);
System.out.println("sex:" + sex);
System.out.println("major:" + major);
System.out.println("like:" + s);
Part part = req.getPart("myFile");
String cd = part.getHeader("Content-Disposition");
// System.out.println(cd);
String filename = cd.substring(cd.lastIndexOf("=") + 2, cd.length() - 1);
// System.out.println(filename);
String filename1 = UUID.randomUUID().toString() + ".jpg";
// System.out.println(filename1);
part.write(filename1);
req.setAttribute("filename",filename1);
req.setAttribute("username", username);
req.setAttribute("password", password);
req.setAttribute("sex", sex);
req.setAttribute("major", major);
req.setAttribute("like", like);
req.getRequestDispatcher("regSuccess.jsp").forward(req, resp);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setContentType("text/html;charset=UTF-8");
// request.setCharacterEncoding("UTF-8");
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册成功</title>
</head>
<body>
<p style="color: #ff0000">${username}</p>
<ul>
<li>用户信息</li>
<li>用户名:${username}</li>
<li>密 码:${password}</li>
<li>性 别:${sex}</li>
<li>专 业:${major}</li>
<li>兴 趣:${like}</li>
<li><img src="D:/jerbrain/img/${filename}" width="200px"/></li>
<li><a href="reg.jsp">返回登入页面</a></li>
</ul>
</body>
</html> 前面大家给的方法试过了还是不行,本人配置了一下虚拟路径最后成功了。如果大家使用idea出现404路径找不到,也可以使用虚拟路径,因为浏览器为了安全是不让访问本地文件的。
你说的第五行是哪个?这 jsp 代码和 servlet 代码 都在
有可能是El 的失效问题,在jsp文件头部添加
<%@ page isELIgnored="false" %>
Twilight6 发表于 2022-10-13 22:12
你说的第五行是哪个?这 jsp 代码和 servlet 代码 都在
最后面 应该是el失效了,
<%@ page isELIgnored="false" %>
加一条这个看下,然后再浏览器看下路径,是否正确
页:
[1]