JSP 客户端测试 Servlet 部署 RESTful 网络服务
JSP client to test Servlet-deployed RESTful webservice
我最近使用 restlet.org 框架在 Eclipse 中创建了一个简单的 servlet 部署的 web 服务(使用 Tomcat),我可以使用它在浏览器中显示文本文件中的文本。我还创建了一个具有简单形式的 JSP (input.jsp):一个文本框和一个按钮。当用户输入一个词并单击按钮时,第二个 JSP (result.jsp) 应该打开,仅显示包含该词的文本文件的短语。
我想使用这些 JSP 构建一个测试客户端来测试我的网络服务,但我真的不知道该怎么做。这两个 JPS 当前都位于我项目的 /WEB-INF 文件夹中,但是如果我尝试 运行 我服务器上的第一个 JSP,则会弹出一条错误消息:"The server has not found anything matching the request URI" .只需导航到下面的 QuotesApplication class 中定义的 URI 就可以很好地工作。
申请class:
package edu.ap.rest;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
public class QuotesApplication extends Application {
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/quotes", QuotesResource.class);
router.attach("/quotes/{searchTerm}", QuotesSearchResource.class);
return router;
}
}
资源 1:
package edu.ap.rest;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;
import edu.ap.txt.TXTParser;
public class QuotesResource extends ServerResource {
@Get("html")
public String getQuotes() {
TXTParser parser = new TXTParser();
return parser.getQuotes();
}
}
资源 2:
package edu.ap.rest;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;
import edu.ap.txt.TXTParser;
public class QuotesSearchResource extends ServerResource {
@Get("html")
public String getSearchQuotes(String searchTerm) {
searchTerm = getAttribute("searchTerm");
TXTParser parser = new TXTParser();
return parser.getSearchQuotes(searchTerm);
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>first steps servlet</display-name>
<!-- Application class name -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>
edu.ap.rest.QuotesApplication
</param-value>
</context-param>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
输入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">
<title>Zoek een quote</title>
</head>
<body>
<form method="POST" action="Result.jsp">
Zoekterm: <input type="text" name="searchTerm"><br/>
<br/><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Result.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="edu.ap.rest.*"%>
<!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">
<title>Resultaat</title>
</head>
<%
QuotesSearchResource resource = new QuotesSearchResource();
String searchTerm = ("searchTerm");
System.out.println("Test: " + searchTerm);
String Result = resource.getSearchQuotes(searchTerm);
%>
<body>
<%= Result %>
</body>
</html>
JSP 也是一个 servlet。
您错过了这样的部分:
<servlet>
<servlet-name>QuoteInputServlet</servlet-name>
<jsp-file>/Input.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>QuoteInputServlet</servlet-name>
<url-pattern>/input</url-pattern>
</servlet-mapping>
我最近使用 restlet.org 框架在 Eclipse 中创建了一个简单的 servlet 部署的 web 服务(使用 Tomcat),我可以使用它在浏览器中显示文本文件中的文本。我还创建了一个具有简单形式的 JSP (input.jsp):一个文本框和一个按钮。当用户输入一个词并单击按钮时,第二个 JSP (result.jsp) 应该打开,仅显示包含该词的文本文件的短语。
我想使用这些 JSP 构建一个测试客户端来测试我的网络服务,但我真的不知道该怎么做。这两个 JPS 当前都位于我项目的 /WEB-INF 文件夹中,但是如果我尝试 运行 我服务器上的第一个 JSP,则会弹出一条错误消息:"The server has not found anything matching the request URI" .只需导航到下面的 QuotesApplication class 中定义的 URI 就可以很好地工作。
申请class:
package edu.ap.rest;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
public class QuotesApplication extends Application {
/**
* Creates a root Restlet that will receive all incoming calls.
*/
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/quotes", QuotesResource.class);
router.attach("/quotes/{searchTerm}", QuotesSearchResource.class);
return router;
}
}
资源 1:
package edu.ap.rest;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;
import edu.ap.txt.TXTParser;
public class QuotesResource extends ServerResource {
@Get("html")
public String getQuotes() {
TXTParser parser = new TXTParser();
return parser.getQuotes();
}
}
资源 2:
package edu.ap.rest;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;
import edu.ap.txt.TXTParser;
public class QuotesSearchResource extends ServerResource {
@Get("html")
public String getSearchQuotes(String searchTerm) {
searchTerm = getAttribute("searchTerm");
TXTParser parser = new TXTParser();
return parser.getSearchQuotes(searchTerm);
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>first steps servlet</display-name>
<!-- Application class name -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>
edu.ap.rest.QuotesApplication
</param-value>
</context-param>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
输入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">
<title>Zoek een quote</title>
</head>
<body>
<form method="POST" action="Result.jsp">
Zoekterm: <input type="text" name="searchTerm"><br/>
<br/><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Result.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="edu.ap.rest.*"%>
<!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">
<title>Resultaat</title>
</head>
<%
QuotesSearchResource resource = new QuotesSearchResource();
String searchTerm = ("searchTerm");
System.out.println("Test: " + searchTerm);
String Result = resource.getSearchQuotes(searchTerm);
%>
<body>
<%= Result %>
</body>
</html>
JSP 也是一个 servlet。 您错过了这样的部分:
<servlet>
<servlet-name>QuoteInputServlet</servlet-name>
<jsp-file>/Input.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>QuoteInputServlet</servlet-name>
<url-pattern>/input</url-pattern>
</servlet-mapping>