请求的资源不可用错误

时间:2015-06-08 06:28:21

标签: java jsp servlets twitter4j

我正在尝试使用jsp,servlet,Tomcat-6.0.43和eclipse构建一个twitter搜索,并获取HTTP Status 404错误。任何人都可以检查我哪里出错了。 我的代码:

first.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="ServletValues.java" method="get">
Enter Twitter Search Details    : <input type="text" name="first"><br>
<input type="submit">
</form>
</body>
</html>

TwitterServlet.java:

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;


public class TwitterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public TwitterServlet() {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String CONSUMER_KEY = "[data]";
        String CONSUMER_KEY_SECRET = "[data]";
        String AccessToken = "[data]";
        String AccessTokenSecret = "[data]";
        response.setContentType("text/html");
        String input1 = request.getParameter("first");

        try{
        Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);


            AccessToken oathAccessToken = new AccessToken(AccessToken, AccessTokenSecret);

            twitter.setOAuthAccessToken(oathAccessToken);
            List<Status> status = twitter.getUserTimeline(input1);
            for (Status status2 : status)
            {
                System.out.println("---Tweet---"+status2.getText());
            }}catch (TwitterException te){
                System.out.println("Error occured "+te);
            }




        super.doPost(request, response);
    }

}

的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>
    Twitter12</display-name>
    <servlet>
        <description>
        </description>
        <display-name>
        TwitterServlet</display-name>
        <servlet-name>TwitterServlet</servlet-name>
        <servlet-class>
        TwitterServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TwitterServlet</servlet-name>
        <url-pattern>/TwitterServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>
  

错误:HTTP状态404 - /Twitter12/ServletValues.java

     

输入状态报告

     

message /Twitter12/ServletValues.java

     

说明请求的资源不可用。

2 个答案:

答案 0 :(得分:0)

将表单操作更改为:

<form action="/TwitterServlet" method="get">

您的servlet网址是/已定义的/ TwitterServlet

<servlet-mapping>
    <servlet-name>TwitterServlet</servlet-name>
    <url-pattern>/TwitterServlet</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

在您的jsp文件表单操作中,替换

<form action="ServletValues.java" method="get">

使用

<form action="TwitterServlet" method="get">

因为在您的web.xml中,您要调用的Servlet类的servlet映射模式为TwitterServlet

同样在你的servlet中你只是打印到System.out,而你应该使用

写入响应流
response.getOutputStream().write("---Tweet---"+status2.getText());

以便它显示在回复中