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 .
Related
I am getting blank page as output in my web browser when I run DemoServlet
note : I have used both doGet and doPost methods in my servlet instead of service method even though I am getting the same output.
The below is the following code
public class Student_Class {
int RollNo = 0;
String Name="";
Student_Class(int RollNo,String Name) {
this.RollNo = RollNo;
this.Name = Name;
}
public int getRollNo() {
return RollNo;
}
public String getName() {
return Name;
}
}
the above is my StudentClass
I have created a servlet like below
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet {
#Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student_Class Student = new Student_Class(1,"Name1");
request.setAttribute("Student", Student);
RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
}
and my jsp file is below the name of my jsp file is : display.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix= "c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${Student}
</body>
</html>
my web.xml file is like below
<?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/j2ee" xmlns:web="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_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.5">
<display-name>JSTL_Demo</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>
<description></description>
<display-name>Demo</display-name>
<servlet-name>Demo</servlet-name>
<servlet-class>Demo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo</servlet-name>
<url-pattern>/Demo</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DemoServlet</display-name>
<servlet-name>DemoServlet</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/DemoServlet</url-pattern>
</servlet-mapping>
</web-app>
I am using tomcat 10.0.10 as my server and eclipse as my IDE
please help me I am a beginner and I don't know much about jsp and servlets correct me if I have done anything wrong
click here to see the output on my browser
click here to see the project explorer
I'm trying to set a list into request attribute to print it in the jsp file. but getAttrubute in showMentors.jsp giving null value.
is the problem is the controller or web.xml?
any help please?
I think the controller dose not working.
Controller.java
#WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
private static SessionFactory factory = null;
//set managers
Mentors mentorsManager = Mentors.getInstance();
public static SessionFactory getSessionFactroy() {
try{
if(factory == null)
factory = new AnnotationConfiguration().configure().buildSessionFactory();
}
catch(HibernateException e){
System.err.println(e.getMessage());
}
finally{
return factory;
}
}
/**
* #see HttpServlet#HttpServlet()
*/
public Controller() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();//writer of the response
String pathBuffer = request.getPathInfo();//the url
switch(pathBuffer){ //switch for the url
case "/showMentors":
List<Mentor> c = mentorsManager.getAllMentors();
request.getSession().setAttribute("mymentors", c); // sending the coupons that the view will use
request.getServletContext().getRequestDispatcher("showMentors.jsp").forward(request, response);
break;
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.1"
metadata-complete="false"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>WebPerachProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.sakhnin.implementations.Controller</servlet-class>
</servlet>
<servlet>
<servlet-name>index</servlet-name>
<jsp-file>/jspFiles/index.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>Mentor</servlet-name>
<jsp-file>/jspFiles/Mentor.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>showMentors</servlet-name>
<jsp-file>/jspFiles/showMentors.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/jspFiles/index.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>showMentors</servlet-name>
<url-pattern>/jspFiles/showMentors.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Mentor</servlet-name>
<url-pattern>/jspFiles/Mentors.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/jspFiles/*</url-pattern>
</servlet-mapping>
</web-app>
showMentors.jsp
<%#page import="java.util.List"%>
<%# page import="com.sakhnin.classes.*"%>
<%# page import="com.sakhnin.implementations.*"%>
<%# page language="java" contentType="text/html; charset=windows-1255"
pageEncoding="windows-1255"%>
<!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=windows-1255">
<title>All mentors</title>
<link type="text/css" rel="stylesheet" href="../styleFile/ourStyle.css" />
<link type="text/css" rel="stylesheet" href="../styleFile/bootstrap.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
</head>
<body>
<div data-role="header" data-theme="b">
<h1>Show mentors page</h1>
<div data-role="navbar">
<ul>
<li>all mentors </li>
</ul>
</div>
</div>
<!-- Display a coupons page -->
<div data-role="content" data-theme="b">
<table class="table table-hover table-striped" id="mentors" height="100%" width="100%" border="3px" bordercolor="blue">
<thead>
<tr style="color: green;">
<th>fullname</th>
</tr>
</thead>
<tbody>
aaaaaaaaaaaaaaa
hhhhhhhhhhhhhhhh
<%
// Retrieves the first page and display it
//List<Mentor> m = (List<Mentor>)request.getAttribute("mymentors");
List<Mentor> m = (List<Mentor>)request.getSession().getAttribute("mymentors");
System.out.println(m);
%>
<%
if (m!=null){
for(Mentor mentor : m) {
%>
<td>cccccccccccccccc<br>
<tr>
<td><%= mentor.getFullName()%><br>
</tr>
<% } }%>
xxxxxxxxxxxxxxxxxxxxx
</tbody>
</table>
</div>
<div data-role="footer" data-theme="b">
<h4>BY: ASEEL & REMA</h4>
</div>
</body>
</html>
First make sure the client is calling the /Controller first, not the showMentors.jsp
Check if the mentorsManager.getAllMentors(); returns a non-null value, maybe attribute mymentors is set correctly, but the content/value c is null;
And I suggest if it's a request-scope attribute, you may pass the argument/context using requests context, rather than session.
I downloaded an open-source sql injection application. When I tried to run it gives me error "HTTP method POST is not supported by this URL" and some warnings as well. I am sharing my code and some images.
HTML:
<%# 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>Login Page</title>
<form action="userCheck">
<div>
<input type="text" name="user" value=""/>
<input type="submit" value="Submit"/>
</div>
</form>
</head>
<body>
</body>
</html>
XML:
<?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></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>userCheck</servlet-name>
<servlet-class>RKINJ.userCheck</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userCheck</servlet-name>
<url-pattern>/userCheck</url-pattern>
</servlet-mapping>
</web-app>
Java:
package RKINJ;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.*;
import java.sql.Connection;
public class userCheck extends HttpServlet {
protected void processRequest (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("'text/html;charset=UTF-8'");
PrintWriter out = response.getWriter();
try {
String user = request.getParameter("name");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/logindb";
// String dbName = "logindb";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "test1234";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, userName, password);
Statement st = conn.createStatement();
String query = "SELECT * FROM userdetail where id='' + user + ''";
// out.println("Query : " + query);
// System.out.printf(query);
ResultSet res = st.executeQuery(query);
// out.println("Results");
while (res.next()) {
String s = res.getString("name");
out.println("\t\t" + s);
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
} finally {
out.close();
}
}}
These are few warnings I got when run the program
Build path specifies execution environment JavaSE-1.6. There are no JREs installed in the workspace that are strictly compatible with this environment.
Classpath entry C:/Java-Programs/mysql-connector-java-5.1.23-bin-folder.jar/mysql-connector-java-5.1.23-bin.jar will not be exported or published. Runtime ClassNotFoundExceptions may result.
Classpath entry C:/Java-Programs/mysql-connector-java-5.1.23-bin.jar will not be exported or published. Runtime ClassNotFoundExceptions may result.
Invalid location of tag (form). login.jsp /RKINJ/WebContent line 9 JSP Problem.
The serializable class userCheck does not declare a static final serialVersionUID field of type long
You need to override doPost method that in the servlet.
public void doPost(HttpServletRequest req, HttpServletResponse res)
You need to specify the action method
<form action"something" Method="POST / Get">
And you need also to override doPost method that in the servlet.
public void doPost(HttpServletRequest req, HttpServletResponse res)
I wish i was helpfull
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>
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.