I am trying to develop a facebook application using java. Can anyone please help me out?
I have used the code provided in http://www.developer.com/java/article.php/10922_3733751_7/Writing-Facebook-Applications-Using-Java-EE.htm
Here I am giving two servlets and one jsp file and 1 xml file all that I got from the above mentioned site.
AbstractFacebookServlet
public class AbstractFacebookServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
protected static final String FB_APP_URL = "http://apps.facebook.com/myfacebookapp/";
protected static final String FB_APP_ADD_URL = "http://www.facebook.com/add.php?api_key=";
protected static final String FB_API_KEY = "FB_API_KEY";
private static final String FB_SECRET_KEY = "FB_SECRET_KEY";
public AbstractFacebookServlet() {
super();
}
/*
* This method is used by all of the application's servlets (or web
* framework actions) to authenticate the app with Facebook.
*/
protected FacebookRestClient getAuthenticatedFacebookClient(
HttpServletRequest request, HttpServletResponse response) {
Facebook fb = new Facebook(request, response, FB_API_KEY, FB_SECRET_KEY);
String next = request.getServletPath().substring(1);
if (fb.requireLogin(next))
return null;
return fb.getFacebookRestClient();
}
}
And here is the second servlet file
MainPageServlet.java
public class MainPageServlet extends AbstractFacebookServlet implements
javax.servlet.Servlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
FacebookRestClient facebook = getAuthenticatedFacebookClient(request,
response);
if (facebook != null) {
if (getFacebookInfo(request, facebook)) {
request.getRequestDispatcher("/main_page.jsp").forward(request,
response);
}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
/*
* This method obtains some basic Facebook profile information from the
* logged in user who is accessing our application in the current HTTP
* request.
*/
private boolean getFacebookInfo(HttpServletRequest request,
FacebookRestClient facebook) {
try {
long userID = facebook.users_getLoggedInUser();
Collection<Long> users = new ArrayList<Long>();
users.add(userID);
EnumSet<ProfileField> fields = EnumSet.of(
com.facebook.api.ProfileField.NAME,
com.facebook.api.ProfileField.PIC);
Document d = facebook.users_getInfo(users, fields);
String name = d.getElementsByTagName("name").item(0)
.getTextContent();
String picture = d.getElementsByTagName("pic").item(0)
.getTextContent();
request.setAttribute("uid", userID);
request.setAttribute("profile_name", name);
request.setAttribute("profile_picture_url", picture);
} catch (FacebookException e) {
HttpSession session = request.getSession();
session.setAttribute("facebookSession", null);
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
this is the jsp file
main_page.jsp
<%# page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<strong>myacebookapp Main Page</strong>
<div>
<a href="http://www.facebook.com/profile.php?id=${uid}">
<img src="${profile_picture_url}"><br>
</a>
<a href="http://www.facebook.com/profile.php?id=${uid}">
${profile_name}</a>,
you are special because you are using myfacebookapp!
</div>
There is also mentioned that we can also replace the jsp file with the fbml file mentioned below
<%# page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<strong>myacebookapp Main Page</strong>
<div>
<fb:profile-pic uid="loggedinuser"
size="small"
linked="true" /><br>
<fb:name uid="loggedinuser"
useyou="false"
linked="true"
capitalize="true" />,
you are special because you are using myfacebookapp!
</div>
and the web.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>myfacebookapp</display-name>
<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>
Now, what my problem is, When I am accessing the url http://http://localhost:8080/myfacebookapps/main_page.jsp/, it is giving a 404 error.
Can anyone please help me out what I am doing wrong? Actually I am new in this thing.
Your web.xml has no references to your 2 servlets that you've implemented.
Add something like this to your web.xml. The descriptor must have a <servlet> declaration and a mapping to map to the servlet (called <servlet-mapping>)
Example:
<servlet>
<description>Abstract Facebook Servlet</description>
<display-name>AbstractFacebookServlet</display-name>
<servlet-name>AbstractFacebookServlet </servlet-name>
<servlet-class>AbstractFacebookServlet </servlet-class>
</servlet>
<servlet>
<description>Main Page Servlet Servlet</description>
<display-name>MainPageServlet</display-name>
<servlet-name>MainPageServlet </servlet-name>
<servlet-class>MainPageServlet </servlet-class>
</servlet>
Make sure your <servlet-class> is exactly a fully-qualified servlet class.
And your mappings to your servlet,
<servlet-mapping>
<servlet-name>AbstractFacebookServlet</servlet-name>
<url-pattern>/facebook/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MainPageServlet</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
Further Resources:
Java Servlet Technology.
Related
I've tried all methods available on StackOverflow and the internet, but nothing seems to workout as intended, below's a list of what I've tried doing:
All declared methods in my java files are protected named doPost()
I tried using sendRedirect() method instead of RequestDispatcher()
I also tried using another method and calling it in doPost()
I also tried creating another web project in Eclipse, but that too didn't work
Any help would be appreciated, thanks!
Here is my login.html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="auth" method="post">
<table>
<tr>
<td>Email ID:</td>
<td><input type="email" name="userEmail"></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="userPassword"></td>
</tr>
<tr>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
authenticate.java
package newAuthentication;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class authenticate extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userEmail = request.getParameter("userEmail");
String userPassword = request.getParameter("userPassword");
if (validateUser.checkUser(userEmail, userPassword))
{
HttpSession session = request.getSession();
session.setAttribute("userEmail", userEmail);
RequestDispatcher dispatch = request.getRequestDispatcher("home");
dispatch.forward(request, response);
}
else
{
out.println("Incorrect authentication credentials, please try again!");
RequestDispatcher dispatch = request.getRequestDispatcher("login.html");
dispatch.include(request, response);
}
}
}
validateUser.java
package newAuthentication;
import java.sql.*;
public class validateUser
{
public static boolean checkUser(String userEmail, String userPassword)
{
boolean state = false;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/wad","root","root");
PreparedStatement fetchData = connect.prepareStatement("select * from studentData where email = ? and password = ?");
fetchData.setString(1, userEmail);
fetchData.setString(2, userPassword);
ResultSet data = fetchData.executeQuery();
state = data.next();
}
catch (Exception err)
{
err.printStackTrace();
}
return state;
}
}
home.java
package newAuthentication;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class home extends HttpServlet
{
protected void doPost(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException
{
doGet(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String userEmail = (String)session.getAttribute("userEmail");
out.println("Welcome user " + userEmail);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>newAuth</display-name>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>newAuthentication.authenticate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/auth</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Home</servlet-name>
<servlet-class>newAuthentication.home</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/home</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>
Why you have this problem in the first place:
This chunk of code is MUCH TOO LONG to try all in one step. Especially if you don't have experience working with servlets. If you are not yet sure if your servlets and basic mappings work, start small - a single servlet, a single GET mapping - that you can try in the browser, without any HTML. Just two files in the entire project: web.xml and MyServlet.java - and just leave the doGet method empty.
Right now, your code is so long, that you simply cannot guess the problem once you hit it - there's too many places to verify!
The magic formula is: "try just one thing at a time".
Solution:
Remove the doGet(request, response) call in your doPost method.
You don not provide your own doGet method, so it defaults to what's in the superclass. It so happens, that the default doGet method throws an exception stating that the GET method is not supported. Since you call it from your doPost, the propagating exception makes container think (rightly so) that it's your doPost that is throwing, and so that it is POST that is unsupported.
Also, the call makes no sense.
BTW: Notice that you would find this error in 5 seconds, if you first tried with the suggested two class approach: you would only have this one thing to verify.
Hi I have a question how to redirect from starting servlet body(doGet() or doPost()) to another's servlet site?
First servlet:
public class StartingServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
PrintWriter out=response.getWriter();
out.println("Strona startowa");
// String go ="http://localhost:8080/HelloWorld/test";
// response.sendRedirect(response.encodeRedirectURL(go));
out.println("<a href=”http://localhost:8080/HelloWorld/test”> Hello World Servlet </a>");
}
}
Second servlet(the one that i want to go after i click on link):
public class HelloWorldServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter out= response.getWriter();
out.println("Hello World Servlet");
}
}
web.xml file(servlet-mappings url-pattern for the starting servlet is empty on purpose):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloWorld</display-name>
<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>
<servlet>
<servlet-name>Starting servlet</servlet-name>
<servlet-class>pl.javastart.servlets.StartingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Starting servlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Hello World Servlet</servlet-name>
<servlet-class>pl.javastart.servlets.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello World Servlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
How do I do that?
Thank you in advance.
To redirect a request from servlet you can use sendRedirect method :
response.sendRedirect("http://localhost:8080/HelloWorld/test");
Btw this statement:
out.println("<a href=”http://localhost:8080/HelloWorld/test”> Hello World Servlet </a>");
will simply send the link in response body but will not redirect.
You can link one servlet to another using href as :
click here
Here servletURL is written as "/nameofproject/urlpattern?"
Other method is getRequestDispatcher which will pass the values also to other servlet/jsp file etc.
request.getRequestDispatcher("/xyz.jsp").forward(request, response);
Hope that helps.
I'm trying to write a servlet using generated HTML code, rather then printing out static HTMLs.
I'm using Eclipse-EE Europa and Tomcat 6.
I tried to use the flowing tips from HERE
But instead of printing the desired attribute It seems that the jsp ignoring the attribute or the attribute is empty.
Here is the servlet:
package com.serv.pac;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* Servlet implementation class for Servlet: testServlet
*
*/
public class testServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
/* (non-Java-doc)
* #see javax.servlet.http.HttpServlet#HttpServlet()
*/
public testServlet() {
super();
}
/* (non-Java-doc)
* #see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String message = "doGet response";
request.setAttribute("message", message);
request.getRequestDispatcher("/testServlet/WEB-INF/index1.jsp").forward(request, response);
PrintWriter out = response.getWriter();
out.println("the servlet");
}
/* (non-Java-doc)
* #see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("first servlet");
}
}
Here is the JSP
<!doctype html>
<html lang="en">
<head>
<title>SO question 2370960</title>
</head>
<body>
<p>Message: ${message}</p>
</body>
</html>
And the following is the :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>servlet1_test</display-name>
<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>index1.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>testServlet</display-name>
<servlet-name>testServlet</servlet-name>
<servlet-class>com.serv.pac.testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
</web-app>
And that is what I'm getting in the browser:
<!doctype html>
<html lang="en">
<head>
<title>SO question 2370960</title>
</head>
<body>
<p>Message: </p>
</body>
</html>
As one may see there is nothing after the "Message" in the html body, as if the message attribute is empty.
Thank You
The only way I can see this happenning is you aren't actually accessing your Servlet.
You've declared a <welcome-file>
<welcome-file>index1.jsp</welcome-file>
So if you try to hit
localhost:8080/YourContextPath
the default Servlet will render and send you that jsp with a missing message attribute.
If you want to hit your actual Servlet, you need to use
localhost:8080/YourContextPath/testServlet
Note that you need to change
request.getRequestDispatcher("/testServlet/WEB-INF/index1.jsp").forward(request, response);
to
request.getRequestDispatcher("/WEB-INF/index1.jsp").forward(request, response);
and move your file under WEB-INF.
I'm trying to write a very simple servlet, and a HTML form's action uses the servlet.
This is my HTML form:
<%# 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>Upload Video</title>
</head>
<body>
<%
Object userSession = session.getAttribute("email");
if(userSession == null)
{
out.println("Welcome to the MyTube, you are not logged in. <br /><br >");
out.println("Please click the following to login: here");
out.println("Or Click this to register: here");
}
else
{
out.println("<form action=\"servletClass\" method=\"post\" enctype=\"multipart/form-data\">");
out.println("<input type=\"file\" name=\"file\" />");
out.println("<input type=\"submit\" />");
out.println("</form>");
}
%>
</body>
</html>
This is my servlet class:
public class uploadServlet extends HttpServlet
{
String awsAccessKey = "WHOOOPS";
String awsSecretKey = "WHOOOPS/YWPkmKfe";
AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
boolean isMulti = ServletFileUpload.isMultipartContent(request);
if (isMulti)
{
ServletFileUpload upload = new ServletFileUpload();
try
{
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext())
{
FileItemStream item = iter.next();
InputStream inputStream = item.openStream();
if (item.isFormField())
{
}
else
{
String fileName = item.getName();
if (fileName != null && fileName.length() > 0)
{
S3Service s3Service = new RestS3Service(awsCredentials);
S3Object fileObject = new S3Object();
fileObject.setDataInputStream(inputStream);
fileObject.setContentLength(Integer.parseInt(request.getHeader("Content-Length")));
s3Service.putObject("vidvidbucket", fileObject);
//read stream of file uploaded
//store as a temporary file
//upload the file to s3
}
}
}
}
catch (Exception e)
{
}
}
response.sendRedirect("location of the result page");
}
}
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyTubeServer</display-name>
<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>
<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>uploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletClass</servlet-name>
<url-pattern>/servletClass</url-pattern>
</servlet-mapping>
</web-app>
This is the eclipse settings:
I looked at other posts that you should also include the java file at your WEB-INF folder. But I keep getting classNotFoundException.
EDIT: The stacktrace indicates that the uploadServlet cannot be found, so I'm confused.
EDIT 2: After setting XML file for the servlet-class to servletClass.uploadServlet instead of servlet-class, but now I get the requested source, (X/Location%Of%The%File%Is%Not%Found) without any exceptions
Your web.xml is incorrect. The image shows package hierarchy
So, It should be like this
<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>servletClass.uploadServlet</servlet-class>
</servlet>
Restart Tomcat
Second, Try giving context path in form action.
<%
String contextPath = request.getContextPath();
%>
out.println("<form action=\""+contextPath+"/servletClass\" method=\"post\" enctype=\"multipart/form-data\">");
EDIT:
I think now servlet is getting called but This is wrong
response.sendRedirect("location of the result page");
Change it to some valid JSP page.
1) Try this in your web.xml :
<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>servletClass.uploadServlet</servlet-class>
</servlet>
Restart the server and check .
2) The cause of second error is this line :
response.sendRedirect("location of the result page");
in your Servlet code . Please redirect to a proper available resource . For more , refer to the API .
I have home.jsp page and contact page , what concerns us is home.jsp page as follow:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# page contentType="text/html;charset=windows-1252"%>
<%# page import="javax.faces.context.FacesContext" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>home</title>
</head>
<body>
<%
response.sendRedirect("contact.jsp");
%>
</body>
</html>
</f:view>
the web.xml file code:
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/home.jsp</url-pattern>
</servlet-mapping>
</web-app>
when I load home.jsp it return blank page instead of contact.jsp
First at all, you don't really want to have this code in any page never
<%
response.sendRedirect("something.jsp");
%>
If you want to redirect to "contact.jsp" when accessing to "home.jsp", you should do it in a Filter.
public class MyAppFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) respo
String requestPath = httpServletRequest.getRequestURI();
//check if the URL contains <anything>/home.jsp
if (requestPath.contains("home.jsp")) {
//if that's the case, redirect to another page
httpServletResponse.sendRedirect("/SportABAWeb/jsf/Esquema.jsf");
return;
}
}
filterChain.doFilter(request, response);
}
}
You should configure the Filter in your web application. Assuming you use a Java EE 6 web application server (like GlassFish 3.x) or Java EE 6 servlet container (like Tomcat 7), you can add this tag to your class to make it a filter
#WebFilter(filterName = "MyAppFilter", urlPatterns = {"/*"})
public class MyAppFilter implements Filter {
//filter implementation...
}
If not, then you should configure it in your web.xml
<filter>
<filter-name>MyAppFilter</filter-name>
<filter-class>my.package.MyAppFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyAppFilter</filter-name>
<servlet-name>*.jsp</servlet-name>
</filter-mapping>