Tomcat Server in Eclipse: Error 404 submitting to servlet - java

I have set up a tomcat V8.5 server in Eclipse on OSX. I have setup a default servlet to handle get requests from the following html form. I can startup the server and load index.html, but whenever I click submit on the form I receive a 404 error. This code came from a professor and works on her PC, so I am assuming that I have a setup problem with my server on OSX. Does anyone have any idea where I should look first? My professor isn't willing to help diagnose OSX problems. I am not sure exactly what all information I need to provide, happy to provide additional details to anyone willing to help! Thanks in advance!
Picture of my project structure
<form action="http://localhost:8080/CyberFlix0/CyberFlixServlet" method="get">
Film Title: <input type="text" name="film_title"><br>
<input type="submit" value="Submit">
</form>
my doGet function:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
Full Servlet Code:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CyberFlixServlet
*/
#WebServlet("/CyberFlixServlet")
public class CyberFlixServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public CyberFlixServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

Change the value of action tag like this: <form action="/CyberFlixServlet" method="get">

use action="CyberFlixServlet" as shown in bellow
<form action="CyberFlixServlet" method="get">
Film Title: <input type="text" name="film_title"><br>
<input type="submit" value="Submit">
</form>
Please make sure there no Issue exists in your project, for check any issue
open problems view from window -> show view -> problems, if there's any error, fix it
eclipse will reject to compile if there is any problems in your project

I pulled this answer from another post, but this fixes it.
This a problem with tomcat and the catalina config files:
what you have to do is simply:
right click on the server tomcat in eclipse
click on properties
click switch location a little server will appear on the left side in the navigation view
double click on it after you have launched your server
Then select use Tomcat installation and save
this will solve that common 404 problem.

Related

doPost method in servlet not getting called

I am trying to override the doGet and doPost and trying to call the doPost but not working. Below is ths JSP and Servlet code
<%# page language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form method="post" action="FirstServlet">
<input type="text" name="Name" value="Enter">
<input type="button" name="submit" value="Submit"/>
</form>
</body>
</html>
And Servlet code is below and trying to invoke the doPost method but it's not getting called and not printing the message in console. However when i try to access the servlet directly from URL, it's doGet method is getting called
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FirstServlet
*/
#WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public FirstServlet() {
super();
System.out.println(" Inside Constructor");
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println(" Inside init");
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(" Inside doGet");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(" Inside doPost");
doGet(request, response);
}
}
Your HTML form submit button is incorrect: fix type attribute.
Try:
<input type="submit" name="submit" value="Submit"/>

html requested resource not available for servlet [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 5 years ago.
I am trying to connect html to a servlet to get the user input but I get an error that says The requested resource is not available. I even put the servlet on the same folder as the html file, but the error keeps coming. I have the servlet both in Java resources and in html folder (web content). It should be visible. Below is my code for html file and servlet.
html file:
<!DOCTYPE html>
<html>
<!--This is the login page.-->
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
<style>
body {
background-color: lightblue;
backroung-repeat: repeat-1 ;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body background="https://cdn4.iconfinder.com/data/icons/gray-user-management/512/login-512.png" >
<h1>Autentification</h1>
<form action="ConnectionMaker" method="post">
User name:<Input type="text" name="user"><br/><br/>
Password:<Input type="password" name="pass"><br/><br/>
<input type="submit" value="Submit">
login
</form>
</body>
</html>
and servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ConnectionMaker
*/
#WebServlet("/DBConnection")
public class ConnectionMaker extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ConnectionMaker() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String user = request.getParameter("user");
String pass= request.getParameter("pass");
System.out.println(user+" "+pass);
}
}
Change <form action="ConnectionMaker" method="post"> to <form action="DBConnection" method="post"> or maybe /DBConnection (with slash). You hit the URL, not the class name.
Also, why call doGet inside doPost?

Servlet doesn't work after submit button

New to servlets and what not. This is how my project is laid out :
Projectname
src
servlet.java
web content
META-INF
WEB-INF
first.jsp
contents of my servlet.java : pretty much it just takes a text field from jsp
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class servlet
*/
#WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String user=request.getParameter("string");
out.println("<h2> Welcome "+user+"</h2>");
} finally {
out.close();
}
}
}
The 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>Input Form</title>
</head>
<body>
<form action="/servlet" method="POST">
What's your name:<br>
<input type="text" name="string">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and finally the web.xml
<servlet>
<servlet-name>servlet</servlet-name>
<jsp-file>/first.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
The problem is that I can see the basic jsp, which is the form to submit a string on but nothing happens when I click submit. I don't even know where to look for errors and logs to figure out where the problem is.
We had a few issues, that we've discussed at the chat and at the comments:
HttpServlet.service() was being overriden with an empty implementation, so doGet() and doPost() weren't being called.
We had a jsp on web.xml overriding the Servlet annotated mapping.
The reviewed code ended up looking something like this:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
/**
* Servlet implementation class servlet
*/
#WebServlet("/servlet")
public class servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Overriding service() usually isn't needed. - The default implementation mostly
// does the right thingĀ®
super.service(request, response)
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Just forward the request to the jsp page on get requests
request.getRequestDispatcher("/first.jsp").forward(request,response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String user=request.getParameter("string");
out.println("<h2> Welcome "+user+"</h2>");
} finally {
// Don't close the Response - it will mess with filters
// out.close();
out.flush()
}
}
}

how to make servlets send applets to the same html page that has the form

I am very very new to this. and yeah it's a homework.
I am trying to make an HTML page with a form to send some variables to a servlet and the servlet sends an applete to the same form page. so far i succeeded in sending the applet alone. I tried using jQuery, but i couldn't send the HTML tags. I can only send text.
My code so far:
index.html
<html>
<head>
<title> title </title>
</head>
<body>
<form action = "Servlet">
<input type="text" name="Name" value="" size="5" />
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
Servlet.java
package ServletPackage;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
String nm;
nm = request.getParameter ("Name");
PrintWriter mess = response.getWriter();
mess.println("<html>");
mess.println("<head>");
mess.println("</head>");
mess.println("<body>");
mess.println("<b> You've entered Name: </b>" + nm);
mess.println("<p align=center>");
mess.println("<applet code='NameInShape.class' width=200 height=200>");
mess.println("<param name='NAME' value='" + nm + "'>");
mess.println(" </applet>");
mess.println("</p>");
mess.println("</html>");
mess.println("</body>");
mess.close();
}
public static void main( String[] args )
{ }
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
All help appreciated
One way to do it is by using RequestDispatcher to call a .jsp (JavaServer Pages) clone of your index.html with the parameters posted by your form. And don't worry, it's pretty simple to implement. The result will look like the initial page the form still available, but the applet will be called using the specified parameters.
You need to import RequestDispatcher (import javax.servlet.RequestDispatcher;) and then you can do something like this in your doPost method:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("clone.jsp");
rd.forward(request, response);
}
The method rd.forward(request, response); makes the parameters posted to your servlet available on the clone.jsp page, and you can insert them using <%=request.getParameter("ParameterName")%> inside the .jsp page.
For example, after adding the above code in your servlet, you could insert something like this in the clone.jsp page to embed the applet with a parameter received from the index.html form submission:
<applet code=Applet.class width=300 height=300>
<param name=NAME value="<%=request.getParameter("NAME")%>">
</applet>

JSF 2.0 call server side method on page load

let me give you an idea about how the system works.
I am using JAAS login module for login and role management. I can access specific pages depending on the role i have.
I type my url in the address bar, hit enter.
The login page appears and after correct login, it redirects me to the correct page(for now lets call it page1.jsf).
I want to call a server side method on page load.
Can you help me please?
** EDIT **
Assume i have to access page1.jsf which is accessible to role1 only.
In the address bar, i type http://localhost:8080/myapp/page1.jsf
JAAS shows up the login page and after correctly inputting the credentials, i am redirected to page1.jsf
As soon as page1.jsf is requested or on page load, i want to call a server side method from my class to reload page1.jsf
If you are using JSF 2, you can use the above page snippet:
<html xmlns="http://www.w3.org/1999/xhtml"
... >
<f:view contentType="text/html">
<f:event type="preRenderView" listener="#{permissionManager.checkRoles}" />
<f:attribute name="roles" value="ROLE" />
...
</f:view>
</html>
you can add an attribute containing the role and in the PermissionManager.checkRoles() perform redirect to the corret page.
#Named
#ApplicationScoped
class PermissionManager {
...
public void checkRoles(ComponentSystemEvent event) {
String acl = "" + event.getComponent().getAttributes().get("roles");
//Check user role
...
//Redirect if required
try {
ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context
.getApplication().getNavigationHandler();
handler.performNavigation("access-denied");
} catch (Exception e) {
...
}
}
}
Check out this example
and take a look at this related question
Yes this works. Instead of accessing a jsp or jsf page, you can also access Servlets. So create a new servlet. E.g.:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static void yourMethod() {
// do something useful
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
yourMethod();
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Then create a new entry in the web.xml file, in order to map the Servlet to /.
<servlet>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>your.packages.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
After this, you should be able to call localhost:8080/TestServlet which then calls your method.

Categories

Resources