405错误
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!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>Insert title here</title>
</head>
<body>
<form action="<%request.getContextPath(); %>Img_Servlet" method="post" enctype="multipart/form-data">
用户名:<input name="user-name" id="username" type="text"/>
</br>
密码: <input name="user-pass" type="password" id="pass">
<img alt="" src="${pageContext.request.contextPath }/Img_Servlet">
</br><input name="file" id="file" type="file" style="border:1px solid black;" ></br>
<input name="btu" id="btu" type="submit">
</form>
</body>
</html>
package com.imooc.ures;
public class Ures {
private String ures_name;
private String ures_pass;
private String path;
public Ures() {
// TODO Auto-generated constructor stub
}
public Ures(String ures_name, String ures_pass, String path) {
super();
this.ures_name = ures_name;
this.ures_pass = ures_pass;
this.path = path;
}
public String getUres_name() {
return ures_name;
}
public void setUres_name(String ures_name) {
this.ures_name = ures_name;
}
public String getUres_pass() {
return ures_pass;
}
public void setUres_pass(String ures_pass) {
this.ures_pass = ures_pass;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
return "Ures [ures_name=" + ures_name + ", ures_pass=" + ures_pass + ", path=" + path + "]";
}
}
package com.imooc.coll;
import java.util.HashMap;
import java.util.Map;
import com.imooc.ures.Ures;
public interface Coll {
abstract public Map<String, Object> reg(HashMap<String, Object> hashmap,Ures ures);
}
package com.imooc.coll;
import java.util.HashMap;
import java.util.Map;
import com.imooc.ures.Ures;
public class Coll_Uers implements Coll {
@Override
public Map<String, Object> reg(HashMap<String, Object> hashmap,Ures ures) {
hashmap.put(ures.getUres_name(), ures);
return hashmap;
// TODO Auto-generated method stub
}
}
package com.imooc.img;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.imooc.ures.Ures;
/**
* Servlet implementation class Img_Servlet
*/
@WebServlet("/Img_Servlet")
public class Img_Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Img_Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
org.apache.commons.fileupload.disk.DiskFileItemFactory diskFileItemFactory=new org.apache.commons.fileupload.disk.DiskFileItemFactory();
//创建核心类
System.out.println( request.getMethod());
ServletFileUpload sUpload=new ServletFileUpload(diskFileItemFactory);
Map<String, String> ha=new HashMap<>();
try {
List<FileItem> list= sUpload.parseRequest(request);
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
String name =fileItem.getFieldName();
String value=fileItem.getString("UTF-8");
ha.put("name",name);
ha.put("pass", value);
}else {
String path= fileItem.getName();
InputStream is= fileItem.getInputStream();
String absPath="D:/";
String sPath="D:/"+"/"+path;
OutputStream os=new FileOutputStream(sPath);
ha.put("path", sPath);
int len=0;
byte[] b=new byte[1024];
while ((len=is.read(b, 0, len))!=-1) {
os.write(b, 0, len);
}
is.close();
os.close();
}
}
Ures ures=new Ures();
ures.setPath(ha.get("path"));
ures.setUres_name(ha.get("name"));
ures.setUres_pass(ha.get("pass"));
System.out.println(ures);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.jsp页面的img标签src路径什么意思?el表达式不应该是后面有个Scope的吗
正在回答
同学你好。img标签的解释我在https://class.imooc.com/course/qadetail/118788 中回答了哦~
这里只说一下405错误的问题。
有两处需要修改:
1、当需要<%%>为我们输出值时,需要使用<%= %>,并且不能有“;”因为不再是语句而是可以被解析为值的片段。<%request.getContextPath();%>只是一句没有变量名接收的语句,需要改为<%=request.getContextPath %>才能在生成页面时被翻译过来
2、同学设置了form标签的提交请求为Post,但是却只实现了doGet的话,是无法执行doGet中的代码的。可以在doPost中调用一下doGet。
如果解答了同学的疑问,望采纳~
祝学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星