JSP客户端测试Servlet部署的RESTful Web服务

时间:2017-09-01 16:47:23

标签: java web-services jsp servlets restlet

我最近使用restlet.org框架在Eclipse中创建了一个简单的servlet部署的webservice(使用Tomcat),我可以使用它在浏览器中显示文本文件中的文本。我还用一个简单的形式创建了一个JSP(input.jsp):一个文本框和一个按钮。当用户输入单词并单击该按钮时,应打开第二个JSP(result.jsp),仅显示包含该单词的文本文件的短语。

我想使用这些JSP构建一个测试客户端来测试我的网络服务,但我真的不知道如何解决这个问题。这两个JPS当前都位于我项目的/ WEB-INF文件夹中,但是如果我尝试在我的服务器上运行第一个JSP,则弹出一个错误说:"服务器未找到任何与请求URI匹配的内容"。只需导航到下面的QuotesApplication类中定义的URI就可以完美地运行。

申请类:

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:

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13;

的Result.jsp:

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

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>