java 板


LINE

各位好,小弟有两个档案,一个是tryJSP.jsp,另一个是servlet档(TryServlet.java) JSP档里面有个按钮,点下去後会用$.ajax post的方式连到Servlet档 $.ajax post的url是 "/Eclipse专案的名称/TryServlet", Servlet档里面有宣告一个字串(String ss = "Hello"), 然後Servlet会传回该字串到JSP档案,JSP档案会将该字串使用alert的方式显示出来。 只是现在按下去後没有success,而是跑到$.ajax的error区段,且console显示下列讯息: java.lang.ClassNotFoundException: TryServlet 想请问该怎麽解决这个问题,有把url改成"/TryServlet",但结果还是一样 ~"~ 程式码如下: 【TryServlet.java】 import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TryServlet extends HttpServlet { private static final long serialVersionUID = 1L; public TryServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String send_back = "Hello"; PrintWriter out = response.getWriter(); out.print(send_back); out.flush(); out.close(); } } 【tryJSP.jsp】 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script> <title>Insert title here</title> </head> <body> <input type="button" id="query" value="Click me" /> <script type="text/javascript"> $("#query").click(function() { $.ajax({ type : "POST", url : " /MyProjectName/TryServlet", data : { }, dataType : "text", success : function(string_back) { alert(string_back); }, error : function(xhr, textStatus, error) { console.log(xhr); alert("连线错误,请重新查询"); console.log(error); }, //请求完成时执行的函式(不论结果是success或error) complete : function() { alert("done"); } }); }); </script> </body> </html> 【web.xml】 .. .. <servlet> <description></description> <display-name>TryServlet</display-name> <servlet-name>TryServlet</servlet-name> <servlet-class>TryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TryServlet</servlet-name> <url-pattern>/TryServlet</url-pattern> </servlet-mapping> <servlet> .. .. --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 203.208.196.50
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1445307189.A.40D.html
1F:推 Dnight: 你的路径设定在xml还是跟本没设呢? 10/20 10:32
2F:→ qazsd: 路径是设在web.xml,刚加上web.xml的设定文章里了 10/20 11:26
3F:→ ssccg: servlet-class要用全名吧,含package 10/20 11:51
4F:→ qazsd: package是(default package),实体路径上无该资料夹 10/20 13:48
5F:推 Dnight: 你的意思是package放在src资料夹下面吗? 10/20 14:10
6F:推 gpctv: " /MyProjectName/TestServ", ?? 10/20 15:41
7F:→ gpctv: 只是这里打错而已吗 10/20 15:42
8F:推 swallowcc: servlet-class 要打package name, com.ooxx.TryServlet 10/20 16:51
9F:→ qazsd: src目录底下是TryServlet.java 该目录底下无其他资料夹 10/20 23:43
※ 编辑: qazsd (123.195.53.137), 10/20/2015 23:44:30
10F:→ qazsd: 回gpc大大, 已更正post的url为专案名称/TryServlet 10/20 23:45
11F:→ gpctv: 那问题还是一样吗?! 10/21 08:43
12F:→ gpctv: console还是显示ClassNotFoundException吗?! 10/21 08:47
13F:→ qazsd: 对 还是class not found 10/21 16:49
14F:→ gpctv: 假设说你的JQuery url 只打 TryServlet ?!?! 10/21 18:41
15F:→ gpctv: 就”TryServlet” 这样咧?! 10/21 18:42
16F:→ ssccg: 你的TryServlet有compile成.class档吗 10/21 19:42
17F:→ qazsd: 回gpc大大,jQuery url打/TryServlet 10/21 21:44
18F:→ qazsd: 或是打/专案名称/TryServlet好像都不行 10/21 21:44
19F:→ qazsd: 另外没有.class档,按了Eclipse的build project後还是没有 10/21 21:45
20F:→ gpctv: 没有class!!如果你连斜线都不打咧?! 10/21 23:05
21F:→ qazsd: url改成./TryServlet,然後将servlet放在package, 10/22 19:51
22F:→ qazsd: servlet-class改成org.xx.TryServlet後就可以了 10/22 19:52
23F:推 gpctv: good 10/22 20:58
24F:推 Dnight: 好喔那就结案罗~_~ 10/22 21:04







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP