2015-06-08 96 views
0

我試圖建立一個使用jsp,servlet,Tomcat-6.0.43和eclipse並獲取HTTP狀態404錯誤的twitter搜索。任何人都可以請檢查我哪裏錯了。 我的代碼:請求的資源不可用錯誤

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> 

Error: HTTP Status 404 - /Twitter12/ServletValues.java

type Status report

message /Twitter12/ServletValues.java

description The requested resource is not available.

+2

表明了你是什麼意思'ServletValue的效應初探流。 java' here'

'?把'TwitterServlet'放在'ServletValues,Java'上,而不是' – Babel

+0

這是一個愚蠢的錯誤。好好工作。謝謝! – Dhruvil

+0

@Dhruvil請注意您的KeySecret和AccessTokenSecret已被泄露。 –

回答

0

在JSP文件表單操作,替換

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

隨着

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

由於您web.xml你的servlet映射模式對於您打算調用的Servlet類,n是TwitterServlet

另外,在你的servlet你只是打印到System.out而你應該寫爲使用

response.getOutputStream().write("---Tweet---"+status2.getText()); 

,使其在響應

0

更改你的表單動作: ​

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

你的servlet網址是/ TwitterServlet因爲你已經定義

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