我想从jsp文件中调用servlet中的某个方法,但我不知道该怎么做。
以下是jsp:
imageUpload user = new imageUpload();
out.println(clicked);
String img = request.getParameter("picture");
user.insertProfPic(request, response, id, img);
下面是servlet:
public class imageUpload extends HttpServlet implements insertToDB{
private static final String SAVE_DIR = "uploads";
public String dbFileName = "";
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
@Override
public void insertProfPic(HttpServletRequest request, HttpServletResponse response, int user_id, String file) throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Part part = request.getPart(file);//
String fileName = extractFileName(part);//file name
String applicationPath = getServletContext().getRealPath("");
String uploadPath = applicationPath + File.separator + SAVE_DIR;
System.out.println("applicationPath:" + applicationPath);
File fileUploadDirectory = new File(uploadPath);
if (!fileUploadDirectory.exists()) {
fileUploadDirectory.mkdirs();
}
String savePath = uploadPath + File.separator + fileName;
System.out.println("savePath: " + savePath);
String sRootPath = new File(savePath).getAbsolutePath();
System.out.println("sRootPath: " + sRootPath);
part.write(savePath + File.separator);
File fileSaveDir1 = new File(savePath);
dbFileName = SAVE_DIR + File.separator + fileName;
part.write(savePath + File.separator);
try {
Connection conn = DatabaseConnection.getCon();
String query = "insert into images(user_id, images) values(?,?)";
PreparedStatement pst = conn.prepareStatement("");
pst.setInt(1, user_id);
pst.setString(2, dbFileName);
pst.executeUpdate();
out.println("<center><h1>Image inserted Succesfully......</h1></center>");
out.println("<center><a href='display.jsp?id=" + user_id + "'>Display</a></center>");
} catch (Exception e) {
out.println(e);
}
}
private String extractFileName(Part part){
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for(String s : items){
if(s.trim().startsWith("filename")){
return s.substring(s.indexOf("=") + 2, s.length() - 1);
}
}
return "";
}
}
我的dopost方法是空的,我试图从jsp文件调用insertprofpic方法。当我尝试时,它只会给我一个内部服务器错误
暂无答案!
目前还没有任何答案,快来回答吧!