(关于URL)为什么我这样写是错误的?

Tarloy 2001-10-22 04:15:57
在JSP中
=====================================================
String myUrl=request.getRequestURI();
URL url=new URL(myUrl);
String myPath=url.getPath();
===============================================
我这样写是错误的?
出错信息:
========================================================
java.net.MalformedURLException: no protocol: /mail/modules/mailproject/maillist.jsp
at java.net.URL.(URL.java:473)
at java.net.URL.(URL.java:376)
at java.net.URL.(URL.java:330)
at modules.mailproject._0002fmodules_0002fmailproject_0002fmaillist_0002ejspmaillist_jsp_0._jspService(_0002fmodules_0002fmailproject_0002fmaillist_0002ejspmaillist_jsp_0.java:105)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:282)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
at java.lang.Thread.run(Thread.java:484)

==================================================================
...全文
107 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tarloy 2001-10-23
  • 打赏
  • 举报
回复
谢谢
gdsean 2001-10-22
  • 打赏
  • 举报
回复
myUrl="http://" + myUrl
bobokong 2001-10-22
  • 打赏
  • 举报
回复
同意楼上,给你一个方法,用它来转化
如果用url调硬盘上的一个文件也需要用file://...
public static final URL createURL(String fileName) throws Exception
{
URL url = null;
try
{
url = new URL(fileName);
}
catch (MalformedURLException ex)
{
File f = new File(fileName);
try
{
String path = f.getAbsolutePath();
String fs = System.getProperty("file.separator");
if (fs.length() == 1)
{
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
}
path = "file://" + path;
url = new URL(path);
}
catch (MalformedURLException e)
{
System.out.println("Cannot create url for: " + fileName);
throw new Exception(e.getMessage());
}
}
return url;
}
hexiaofeng 2001-10-22
  • 打赏
  • 举报
回复
myUrl 应该写全 如 http://java.sun.com/index.html
andyrew 2001-10-22
  • 打赏
  • 举报
回复
myUrl的型式应该是http://www.csdn.net/expert
cruise_tan 2001-10-22
  • 打赏
  • 举报
回复
加一个处理异常过程:
try{
String myUrl=request.getRequestURI();
URL url=new URL(myUrl);
String myPath=url.getPath();
}
catch(MalformedURLException e){}
flyzhen 2001-10-22
  • 打赏
  • 举报
回复
可能是String myUrl=request.getRequestURI();有问题,你把myUrl输出,看看是否跟你想象的值一样

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧