为什么从Jsp页面跳转不到servlet页面
我的工程名是MyBlogOnline然后在src目录下有一个包com.myblog.Servlet,在这个包里有一个PhotoServlet页面,在WebRoot根目录中有一个admin的文件夹,在这个文件里面还有一个photo文件夹,然后在这个photo文件夹里有一个PhotoUpload.jsp页面。现在就是想通过点击提交跳转到servlet。(在台式电脑那边的eclipse的环境能执行但是在我自己的笔记本电脑那里就是执行不了)代码如下:
servlet代码:
package com.myblog.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.myblog.dao.PhotoDao;
public class PhotoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
//上传图片
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String href="";
SmartUpload su=new SmartUpload();
su.initialize(this.getServletConfig(),request,response);
//设置需上传的图片的最大大小2M
su.setMaxFileSize(2*1024*1024);
//设置只允许上传图片
su.setAllowedFilesList("jpg,gif,bmp");
try {
//上传文件
su.upload();
int count=su.save("/front/photo/pic");
System.out.println(count);
//获取照片描述信息
//在使用jspSmartUpload组件时,内置对象request不能用了
//而是需要使用本组件自带的request来产生一个对象
Request request1=su.getRequest();
String info=request1.getParameter("info");
System.out.print(info);
//获取上传的文件
File file=su.getFiles().getFile(0);
if(!file.isMissing()){
String filename="/front/photo/pic/"+System.currentTimeMillis()+file.getFileExt();
file.saveAs(filename,File.SAVEAS_VIRTUAL);
}
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
}
jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>我的博客后台-上传照片</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<center>
<table width="777" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" style="word-break:break-all;" >
<tr><td colspan="2"><jsp:include page="../view/AdminTop.jsp"/></td></tr>
<tr>
<td><jsp:include page="../view/AdminLeft.jsp"/></td>
<td align="center" valign="top">
<form action="PhotoServlet" method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="8">
<tr height="60"><td colspan="2">【上传照片】</td></tr>
<tr>
<td align="center">选择照片:</td>
<td><input type="file" name="path" size="50"></td>
</tr>
<tr>
<td align="center">照片描述:</td>
<td><input type="text" name="info" size="63"></td>
</tr>
<tr height="50">
<td colspan="2" align="center">
<input type="submit" value="上传" class="btn_bg">
<input type="reset" value="重置" class="btn_bg">
</td>
</tr>
</table>
</form>
</td>
</tr>
<tr><td colspan="2"><jsp:include page="../view/AdminEnd.jsp"/></td></tr>
</table>
</center>
</body>
</html>
最后的错误是:
HTTP Status 404 - /MyBlogOnline/admin/photo/PhotoServlet
--------------------------------------------
type Status report
message /MyBlogOnline/admin/photo/PhotoServlet
description The requested resource (/MyBlogOnline/admin/photo/PhotoServlet) is not available.
--------------------------------------------
Apache Tomcat/6.0.18
关于这个路径问题我都纠结几天了,求大神指教
[解决办法]
404是路径错误
/MyBlogOnline/admin/photo/PhotoServlet
1,web.xml必须设置PhotoServlet映射,并且必须设置正确,如
<servlet>
<servlet-name>xx</servlet-name>
<servlet-class>com.myblog.servlet.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xx</servlet-name>
<url-pattern>/admin/photo/PhotoServlet</url-pattern>
</servlet-mapping>
2,MyBlogOnline是虚拟目录,你的server.xml里面Context设置的有这个吗
[解决办法]
你这是提交到PhotoServlet吗。你这是photoUploadResult.jsp
啊
[解决办法]
2楼说的方法很对,路径没有设置正确,你的<servlet-mapping>
<servlet-name>xx</servlet-name>
<url-pattern>/admin/photo/PhotoServlet</url-pattern>
</servlet-mapping>
缺少这个或者url-pattern里面写错了。