Call JSP from servlet [duplicate] - java

This question already has answers here:
getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever?
(3 answers)
Calling a servlet from JSP file on page load
(4 answers)
Closed 1 year ago.
Error ->
HTTP Status 405 - HTTP method GET is not supported by this URL
type Status report
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
web.xml
<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 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_3_0.xsd"
version="3.0">
<display-name>To do List</display-name>
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
</web-app>
LoginServlet.java
package com.todo;
//imports ->
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// url ->
#WebServlet(urlPatterns = "/login.do")
// response genrator ->
public class LoginServlet extends HttpServlet{
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {{
// JSP code response ->
RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/WEB-INF/veiws/login.jsp");
RequetsDispatcherObj.forward(request, response); }
}
}
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>hello world from jsp</title>
</head>
<body>
<p>
Hello world from jsp!
</p>
</body>
</html>

I think you should create a method doGet(...) for a GET request.
If both GET and POST do the same things, you can create another method and call it from doPost and doGet.
Have a nice day

Related

Tomcat - Error instantiating servlet class - intelij

After creating a project in intelij and starting Tomcat Server I successfully get my index.jsp page.
But when I try to open HelloServlet.java I get: "Error instantiating servlet class" error.
And then when I reload this page I get the 404 error: "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
This code definitely works on another PC with Tomcat, but doesn't on mine.
HelloServlet.java
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%# page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
<br/>
Hello Servlet
</body>
</html>
Project Structure
|____resources
|____webapp
| |____index.jsp
| |____WEB-INF
| | |____web.xml
|____java
| |____HelloServlet.java
What should I do to fix my problem?
I fixed my problem installing Tomcat 8.5.70
I don't know what was wrong with Tomcat 10, but 8.5.70 works fine with the same project.
UPD:
The problem was in imports. Tomcat 10 uses the jakarta.* package rather than javax.*.
Thanks every one for your advises.

HTTP Status 404 - Not Found; type Status report; message Not Found; description The requested resource is not available [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 2 years ago.
please before giving me any negative vote. wait... I am very new to this .... and i am unable to solve error...
I am getting the following error on my java servlet first program; Name of servlet class is Anjali and servlet url and name is vashisth.
the error come is this:
HTTP Status 404 - Not Found
type Status report
messageNot Found
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 5.1.0
the xml code is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<servlet>
<servlet-name>vashisth</servlet-name>
<servlet-class>Anjali</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vashisth</servlet-name>
<url-pattern>/vashisth</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
the html code is:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<form name="loginForm" method="get" action="Anjali">
Username: <input type="text" name="username"/> <br/>
Password: <input type="password" name="password"/> <br/>
<input type="submit" value="Login" />
</form>
</div>
</body>
</html>
and my java program is this:
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;
#WebServlet("/Anjali")
public class Anjali extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// read form fields
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: " + username);
System.out.println("password: " + password);
// do some processing here...
// get response writer
PrintWriter writer = response.getWriter();
// build HTML code
String htmlRespone = "<html>";
htmlRespone += "<h2>Your username is: " + username + "<br/>";
htmlRespone += "Your password is: " + password + "</h2>";
htmlRespone += "</html>";
// return response
writer.println(htmlRespone);
}
}
Please help me find the error ...tomorrow is the last date of submission of similar assignments... and my whole went rectifying many such types of errors...
please please please
In you html code you have mentioned method="get" and in your java class Anjali.java
you have written code to accept post request only that's why you are getting 404. As you request types are not matching.
Solution:Please change method="get" ---> method="post"
I hope this will solve your problem.

Servlet Not Found Error [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
Im getting a servlet not found error. Im using WIldFly. My directory structure looks like this:
root --> app, converter.html, src
app --> WEB-INF
WEB-INF--> classes, lib, web.xml
src --> servlet.java
I have been looking over it for awhile and can't pin point the problem. I think I have the mapping down correctly in the web.xml and the form action seems to be sent to the right place as well in the .html file.
Servlet Class:
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servlet extends HttpServlet{
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String username = request.getParameter("username");
String email = request.getParameter("email");
response.getWriter().println("<html>");
response.getWriter().println("<head>");
response.getWriter().println("<title>Title</title>");
response.getWriter().println("</head>");
response.getWriter().println("<body>");
response.getWriter().println("Convert. ");
response.getWriter().println("</body>");
response.getWriter().println("</html>");
}
}
web.xml
<web-app 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"
version="2.4">
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>
converter.html
<!DOCTYPE html>
<html>
<head>
<title> Test form </title>
</head>
<body>
<form action="http://localhost:8080/root/src/servlet" method="get">
Name: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
You need to fix the converter.html in the following ways:
Change the action to action="servlet"
Change the method from GET to POST since you want to send data to the server. The HTTP GET method is sued for retrieving data from the server.
You can read more on this link below:
http://www.tutorialspoint.com/servlets/servlets-form-data.htm

Java Servlet error 'Resource Not Available'

I'm trying to build a Java servlet, and I've done everything according to the instructions my prof gave our class, but I'm getting a weird error.
Background: I'm working with Java EE Helios and Tomcat 7.
I started a new dynamic web project in Eclipse, I made an index.jsp page that just gets the user's name and sends it to the servlet, and is then supposed to print out Hello, [username]. The code is all sample code that the prof gave us and works for other people in my class.
I made a new Servlet called ServletHome and it's in a package called servlets.
When I run the program from Eclipse, it starts up Tomcat fine, no problems. I can navigate to the index.jsp page, and it looks fine.
The problem is that when I fill in my name and press my 'submit' button, I get a tomcat 404 error with the message:
"The requested resource (/MyFirstServlet/ServletHome) is not available."
Any ideas?
Thanks!!
--- Edit: Code ---
index.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="ServletHome" method="POST">
First Name: <input type="text" name="firstName" size="20"><br>
Last Name: <input type="text" name="lastName" size="20"> <br>
<br> <input type="submit" value="Submit">
</form>
</body>
</html>
ServletHome.java:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHome extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletHome() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
/**
* The function that gets the name and returns an HTML file with Hello to them
* #param request
* #param response
* #throws ServletException
* #throws IOException
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//set type of output
response.setContentType("text/html;charset=UTF-8");
//get writer
PrintWriter out = response.getWriter();
//get first name
String firstName = request.getParameter("firstName").toString();
//get last name
String lastName = request.getParameter("lastName").toString();
//write the file
out.println("<html>");
out.println("<head>");
out.println("<title>Test Title</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>Welcome, " + firstName + " " + lastName + "!</p>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>ServletHome</servlet-name>
<servlet-class>servlets.ServletHome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletHome</servlet-name>
<url-pattern>/servlets/*</url-pattern>
</servlet-mapping>
</web-app>
The resource is no available because:
Your action attribute is wrong, or;
You didn't map your servlet corretly. To do this, you can:
Use #WebServlet annotation, since you are using Tomcat 7:
#WebServlet( name = "ServletName", urlPatterns = { "/path/to/your/servlet/myName" } )
public class ServletName extends HttpServlet {
// your code here
}
Or map your servlet in web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 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_3_0.xsd">
<!-- more code here... -->
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>yout.package.here.ServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/path/to/your/servlet/myName</url-pattern>
</servlet-mapping>
<!-- more code here... -->
</web-app>
Another thing that you need to pay attention is that you MUST implement the doXXX methods that corresponds to the HTTP methods (get, post, etc.) that you want your servlet to serve.
To request this servlet through you form, you need to set the action attribute as:
<form action="/path/to/your/servlet/myName">
<!-- form fields here... -->
</form>
To finish, you can use the method attribute in your form to choose the HTTP method that your browser will use to request the Servlet. If you don't provide, the default method is get. As I already said, if the get method is used, you need to implement the doGet method in the servlet.

tomcat requested resource () is not available [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
i know its a very common question as i find many questions relating to this in several forums, including SO. but i have not found a solution yet
my web.xml (located in WEB-INF)
<?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>SMSProjectNew</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ReceiveMessagesServlet</display-name>
<servlet-name>ReceiveMessagesServlet</servlet-name>
<servlet-class>com.sendreceive.ReceiveMessagesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReceiveMessagesServlet</servlet-name>
<url-pattern>/ReceiveMessagesServlet</url-pattern>
</servlet-mapping>
</web-app>
the html page index.html, located in WebContent folder
<!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>
The application started successfully version 1:27
<form action="/ReceiveMessagesServlet" method="post">
<input type="text" name="number"/>
<input type="text" name="message"/>
<input type="submit" name="submit"/>
</form>
</body>
</html>
finally the servlet, ReceiveMessagesServlet, located in src\com.sendreceive
package com.sendreceive;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReceiveMessagesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ReceiveMessagesServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) {
String responseMessage = request.getParameter("message");
String responseNumber = request.getParameter("number");
System.out.println(responseMessage+responseNumber);
}
}
i have installed the tomcat plugin in eclipse. when i right click on the project and then click on run the project on server. tomcat server starts in eclipse and the index.html page is shown..but when i enter some values in the fields and click submit..it gives the 404 error..i have been struggling from past 2 hours..kindly help..also..fyi, i am using this tutorial
http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html
You are getting 404 error because of the action="/ReceiveMessagesServlet", please remove the slash. Try with action="ReceiveMessagesServlet".
When you add a slash to URL pattern then the container will look for a web application deployed with name 'ReceiveMessagesServlet'.Since this not there you will receive 404 error.
When you deploy your application into servlet container, your URLs may be prefixed by the context path identifying your application among other applications in that container (i.e. /ReceiveMessagesServlet becomes /MyApp/ReceiveMessagesServlet).
Therefore you should take that possibility into account and modify your URLs accordingly, for example, with JSTL's <c:url>:
<form action="<c:url = value = '/ReceiveMessagesServlet' />" method="post">
Alternatively, without JSTL:
<form action="${pageContext.request.contextPath}/ReceiveMessagesServlet" method="post">
Errors in your servlet may cause Tomcat to mark it as unavailable as I got in my server log:
06/02/2013 13:32:43 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Imager as unavailable 06/02/2013 13:32:43
org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate
exception for servlet Imager java.lang.ClassNotFoundException com.project.test.ImageThumber
You should look the log for reasons.
In my case a missing jar with some test classes.

Categories

Resources