我正在使用Eclipse IDE并正在编写一个servlet.servlet应该接受来自html文件的值并相应地返回JSON响应.
我的doPost()是:
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try
{
res.setContentType("application/json");
res.setHeader("Cache-Control", "nocache");
res.setCharacterEncoding("utf-8");
PrintWriter out = res.getWriter();
JSONObject json = new JSONObject();
String un=req.getParameter("uname");
String pw=req.getParameter("password");
if(un.equals("xxx") && pw.equals("xxx"))
json.put("result", "success");
else
json.put("result", "fail");
out.print(json.toString());
}
catch(Exception e){System.out.print( e.getMessage());}
}
Run Code Online (Sandbox Code Playgroud)
当我在Eclipse中运行这个servlet时,我得到一个文件下载对话框.
当使用Tomcat在Eclipse外部运行时,我收到错误:
root cause
java.lang.NoClassDefFoundError: org/json/JSONObject
Server1.doPost(Server1.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.lang.ClassNotFoundException: org.json.JSONObject
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
Server1.doPost(Server1.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Run Code Online (Sandbox Code Playgroud)
Server1.doPost(Server1.java:25)行引用
JSONObject json = new JSONObject();
Run Code Online (Sandbox Code Playgroud)
我已将org.json.jar添加到构建路径中,并将其添加到部署路径中 Properties->Configure build path->Deployment assembly
我想在 WAS8.5.5.2 中部署一个应用程序。我用结构做了耳朵:
serverEar.ear
-- serverWar.war
---- WEB-INF
---- 'web' 目录内容
我有application.xml :
<application version="6" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" >
<module>
<web>
<web-uri> serverWar.war</web-uri>
<context-root>serverWar</context-root>
</web>
</module>
<library-directory> web/lib</library-directory>
</application>
Run Code Online (Sandbox Code Playgroud)
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>p1</servlet-name>
<servlet-class>com.test.Server</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>p1</servlet-name>
<url-pattern>/server</url-pattern>
</servlet-mapping>
<security-constraint>
<display-name/>
<web-resource-collection>
<web-resource-name/>
<description/>
<url-pattern>/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<user-data-constraint>
<description/>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我有目录结构:
我已经从 WAS ibm/console 安装了该应用程序。安装目录就是..\Desktop\Test\out\artifacts\serverEar我的ear文件所在的地方(不知道安装目录会不会影响这个?)。
应用程序已成功安装并启动。
我已经尝试过网址:https://my_server:9443/serverWar/server,它给了我:Error 404: java.io.FileNotFoundException: SRVE0190E: File not found: …