I have a simple program which get username and password, if the password is servlet then it will forward to the welcome page, else, it will show a message and includes html login form.
When I enter username and password and click on login, no matter what, it always shows me the "HTTP Status 405 : HTTP method GET is not supported by this URL" error.
I have read a lot of questions and answers here, none of them helped me to solve this problem.
Here's my code:
Simple.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class Simple extends HttpServlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String p=request.getParameter("userPass");
if(p.equals("servlet")){
RequestDispatcher rd=request.getRequestDispatcher("welcome");
rd.forward(request, response);
}
else{
out.print("Sorry username or password error!");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request, response);
}
}
}
WelcomeServlet.java
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 WelcomeServlet extends HttpServlet {
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
}
}
index.html
<form action="go" method="post">
Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPass"/><br/>
<input type="submit" value="login"/>
</form>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>Simple</servlet-name>
<servlet-class>Simple</servlet-class>
</servlet>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
Related
I created a test project under Intellij to start Java EE with Tomcat.
My server starts well, no worries aside.
I created a Servlet which contains my HTML code, as well as doPost and getPost.
When I want to run the servlet, the server launches fine but I end up with a blank page.
An idea ?
Code :
package com.octest.servlets;
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 java.io.IOException;
import java.io.PrintWriter;
#WebServlet(name = "Test")
public class Test extends HttpServlet {
protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType ("text/html");
response.setCharacterEncoding ("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\" />");
out.println("<title>Test</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>Bonjour !</p>");
out.println("</body>");
out.println("</html>");
}
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Et le 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>Test</servlet-name>
<servlet-class>com.octest.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>
</web-app>
Thanks
The browser will make a GET request but your HTML page is in doPost method. You need to keep this page inside doGet method.
I m receiving error as "HTTP Status 405 – Method Not Allowed" when trying to execute the below query, I am not sure what I can do to resolve this.
Also, someone could suggest me where can I get flight APIs for free for testing.
package com.Gmaps.web.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Gmaps.web.DAO.GmapsDAOLayer;
import com.Gmaps.web.Model.GmapsModel;
/**
* Servlet implementation class GmUserData
*/
public class GmUserData extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response, String originAutocomplete) throws ServletException, IOException//, NumberFormatException
{
// String uid = request.getParameter("userid");
// GmapsDAOLayer dao = new GmapsDAOLayer();
// GmapsModel model = dao.getUser(uid);
RequestDispatcher rd = request.getRequestDispatcher("UserPage.jsp");
rd.forward(request, response);
// float orgin = Float.parseFloat(request.getParameter("desti"));
String orgin = request.getParameter("place");
GmapsDAOLayer dao = new GmapsDAOLayer();
GmapsModel model = dao.getUser(orgin);
System.out.println("origin: "+orgin);
}
}
And here is my index.html
<html>
<body>
<h2>Hello World!</h2>
<form action="getuserdata" method="get">
<input type= "text" name="userid"><br>
<input type= "text" name="firstname"><br>
<input type="submit">
</form>
</body>
</html>
Here is my web.xml
<!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>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>GmUserData</servlet-name>
<servlet-class>com.Gmaps.web.controller.GmUserData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GmUserData</servlet-name>
<url-pattern>/getuserdata</url-pattern>
</servlet-mapping>
</web-app>
Help would be much appreciated!
try with this <form action="/getuserdata" or just this action="GmUserData"
This question already has answers here:
How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
(16 answers)
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 1 year ago.
I'm using Tomcat8 server and i'm getting following error.
It's url is http://localhost:8080/WeatherWebApp When i'm submitting the details then it's giving this error.
Here is WeatherServlet.java class
package org.akshayrahar;
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 WeatherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
WeatherServlet(){
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("again");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("akshay rahar");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Here is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app 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">
<display-name>WeatherWebApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>WeatherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/CurrentWeather</url-pattern>
</servlet-mapping>
</web-app>
Here is index.html file too:-
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script >
function initMap() {
var input =document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyACZhmkSaIz436Dt3kHt_cVEYKN-gHzfYo&libraries=places&callback=initMap"async defer></script>
</head>
<body>
<h1>Find weather of any city in the world</h1>
<h2>Enter City Name</h2>
<form id="form" action="CurrentWeather" method="GET">
<input id="pac-input" type="text" name="cityname">
</form><br>
<div class="button1">
<button type="submit" form="form" value="Submit">Submit</button>
</div>
</body>
</html>
I've also mentioned stylesheet.css file in comment. Please check it.
The error shows that Tomcat is unable to create an instance of your WeatherServlet class.
You should make its constructor and other methods public too. You can even make use of the default constructor by removing the less accessible constructor:
public class WeatherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WeatherServlet(){
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("again");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("akshay rahar");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Please provide a fully qualified class name on your web.xml. I was facing a similar issue and this is what fixed it.
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>org.akshayrahar.WeatherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/CurrentWeather</url-pattern>
</servlet-mapping>
In my web application I have a main page that contain some information. This page is created by servlet and corresponding jsp file. Almost all other pages in my web application must contain the same information as main page plus some addition information. I don't want dublicate code, so I want to use output of main servlet in other jsp files. Below is a simple example of what I try to accomplish.
This is web.xml file:
<?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>servlet1</servlet-name>
<servlet-class>app.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>app.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
This is java files:
servlet1.java
package app;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
#Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("servletAttribute", 1);
RequestDispatcher view = request.getRequestDispatcher("/servlet1.jsp");
view.forward(request, response);
}
}
servlet2.java
package app;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet2 extends HttpServlet {
#Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("servletAttribute", 2);
RequestDispatcher view = request.getRequestDispatcher("/servlet2.jsp");
view.forward(request, response);
}
}
This is jsp files:
servlet1.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
servlet1
<%
Integer servletAttribute = (Integer)request.getAttribute("servletAttribute");
out.print("<br>servletAttribute:" + servletAttribute);
%>
servlet2.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="/servlet1" />
servlet2
<%
Integer servletAttribute = (Integer)request.getAttribute("servletAttribute");
out.print("<br>servletAttribute:" + servletAttribute);
%>
So servlet2.jsp must display output of servlet1. It display it, but it doesn/t display addition information from servlet2. And I get this error in Log file:
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [servlet2] in context with path [/WebApplication3] threw exception [java.lang.IllegalStateException: Exception occurred when flushing data] with root cause
java.io.IOException: Stream closed
As I understant this error appears because when servlet2.jsp call "/servlet1" servlet1 sent response to client and servlet2.jsp doesn't have session anymore.
So my question is - How can I fix my code to accomplish what I want? Is it possible to include output of some servlet to some jsp file? If it's possible, is it a good or bad practice?
In servlet2.jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="/servlet1" />
In servlet2.jsp, you have used jsp:include.
It is including the response of the servlet1 response.
But the servlet1, it is going to forward the response to another jsp. So that exception occurs.
To avoid this, in Servlet1 class should use view.include(request,response); instead of view.forward(request, response);.
package app;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
#Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("servletAttribute", 1);
RequestDispatcher view = request.getRequestDispatcher("/servlet1.jsp");
view.include(request, response);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting HTTP method POST is not supported by this URL when calling my httpservlet through an html form. I can't see where I'm going wrong. I'm running this on a Tomcat server. Thanks in advance.
The servlet is supposed to print a set of random numbers to the browser.
Here is my servlet:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.RandNumSet;
public class RandomServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public RandomServlet() {
// TODO Auto-generated constructor stub
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
TreeSet<Integer> randNum = RandNumSet.generateRandNumSet();
Iterator<Integer> iterator = randNum.iterator();
String numString = "";
while (iterator.hasNext()){
numString = numString + iterator.next() + " ";
}
out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> "+ numString + " </h1>");
out.println("</body>");
}
}
Here is the calling html form snippet:
<div id ="form">
<form action="RandomServlet" method="post">
<input type="submit" value="Randomize!"/>
</form>
</div>
Here are the web.xml snippets
<servlet>
<display-name>RandomServlet</display-name>
<servlet-name>RandomServlet</servlet-name>
<servlet-class>servlets.RandomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomServlet</servlet-name>
<url-pattern>/RandomServlet</url-pattern>
</servlet-mapping>
I am using the following way and its works...
first: check the directory first you have made like-
com.ServletExample
|-JavaResource
|-src
|-com.servletExample
|-RandomServlet.java
|-WebContent
|-META-INF
|-WEB-INF
|-Lib
|-web.xml
|-index.jsp
second: index.jsp is here
<%# 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>
<div id ="form">
<form action="RandomServlet" method="post">
<input type="submit" value="Randomize!"/>
</form>
</div>
</body>
</html>
third: RendomServlet.java is here
package com.servletExample;
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 RandomServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RandomServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<head>");
out.println("<title>");
out.println("Your Random Numbers!");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Your Random Numbers! </h1>");
out.println("<h1> " + "Hello" + " </h1>");
out.println("</body>");
}
}
fourth: web.xml is here
<?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>JSPExample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>RandomServlet</display-name>
<servlet-name>RandomServlet</servlet-name>
<servlet-class>com.servletExample.RandomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomServlet</servlet-name>
<url-pattern>/RandomServlet</url-pattern>
</servlet-mapping>
</web-app>
I hope this will work.