I'm trying to build a twitter search using jsp, servlet, Tomcat-6.0.43 and eclipse and getting HTTP Status 404 error. Can anyone please check where am I going wrong. My Code:
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.
In your jsp file form action, Replace
With
Since in your
web.xml
your servlet mapping pattern isTwitterServlet
for the Servlet class that you intend to call.Also in your servlet you are just printing to System.out while you should write into the reponse stream using
so that it shows up in the response