This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 1 year ago.
I am learning java maven project, and i just want to know how i can properly connect my post form with my servlet when i submit the form.
i tried to search but can't get the right answer that i need.
I'm working with eclipse and this my folder organization!
this is my servlet:
package servlet;
import java.io.IOException;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
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;
import javax.servlet.http.HttpSession;
#WebServlet(name="Init")
public class Init extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
/*Creation et initialisation du message */
//creation de la session
HttpSession session=request.getSession();
//pseudo user
String name=request.getParameter("pseudo");
session.setAttribute("usersession", name);
//redirection après application du servlet INIT
this.getServletContext().getRequestDispatcher("/interface.jsp").forward(request, response);
}
}
this is my web.xml
<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_3_0.xsd"
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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Init</servlet-name>
<servlet-class>src.main.java.servlet.Init</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Init</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
this is my index.jsp which contains my form:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Chatons !</title>
</head>
<body>
<h1>Bienvenue sur Chatons.org</h1>
<form method="post" action="/java/servlet/Init">
<p>
Entrez votre pseudo :
<input type="text" name="pseudo">
<input type="submit" value="Connexion">
</p>
</form>
</body>
Annotation (#WebServlet in your case) represents the metadata. If you use annotation, web.xml file (deployment descriptor) is not required but you should have Tomcat 7 as it will not run in previous versions of Tomcat server. #WebServlet annotation is used to map the servlet with the specified name.
The web application deployment descriptor web.xml has become optional in Servlet 3.0. Instead, the container at run time will process the annotations of the classes in WEB-INF/classes.
Use #WebServlet(value="/init") instead of yours with the 'name' and get rid of your web.xml descriptor if you prefer annotations.
If you have your src/servlet/Init.java this class is compiled and it will be stored as .class in the following hierarchy:
build/classes/servlet/Init.class
When the container sees the annotation above the class-definition it will check for the .class file in the specified folder and it resolves the call for the required Servlet.
If you have the <form action="init" method="post"> when you hit the submit button the container then searches for the url pattern in the annotation and rest works as said before.
For servlet 3.0 api you don't need a web.xml, you can use annotations instead, have you corrected the action in html
Related
No mapping found for HTTP request with URI [/demoApp/index.jsp] in DispatcherServlet with name 'newApp'
I am getting this error please help.I am using Tomcat v9.0 server.
index.jsp
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit"><br>
</form>
</body>
</html>
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>newApp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>newApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
newApp-servlet.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<mvc:annotation-driven />
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.newApp"></ctx:component-scan>
</beans>
AddController.java
package com.newApp;
import java.lang.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddController {
#RequestMapping("/add")
public void add()
{
System.out.println("I am good");
}
}
program should print "I am good" on the console but it is showing error on browser "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
on the console it is showing "No mapping found for HTTP request with URI [/demoApp/add] in DispatcherServlet with name 'newApp'".
This error accures when i click on submit button on jsp page.
If you want to use view technology like jsp.
You could add bean definition of InternalResourceViewResolver class into your spring configuration file
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
see official docs
I got the same issue and resolved it.
Please add missing folder src/main/java [add package name (for example: com.dev) while adding a class] and then add the java class in that instead of src/main/resource folder. This is the culprit. Let me know for any issue at parixitk28#gmail.com.
I am a spring beginner, I used controller and request mapping to go to
some java file from index.jsp but its showing 404 Error. I am putting
right urlmapping and requestmapping and controllers as I saw in a spring
tutorial.
I have tried including more dependencies, changing the code as I saw
someplace else, but nothing working. Please help me with this code.
Thanks in Advance
index.jsp:
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
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>bhoomika</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bhoomika</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
bhoomika-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="garg.bhoomika"></context:component-
scan>
</beans>
AddController.java
package garg.bhoomika;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddController
{
#RequestMapping("/add")
public void add()
{
System.out.println("I am here");
}
}
Error:
org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringMVC/add] in DispatcherServlet with name 'bhoomika'
For now you should use <mvc:annotation-driven/> in your spring confuguration file to enable MVC configuration as described in official doc
Try to replace entry <context:annotation-config> to <mvc:annotation-driven/>.
And if you want to go to your index.jsp you should change method add() so that it returned view name as a String:
#RequestMapping("/add")
public String add() {
return "index";
}
Also you can see the exhaustive and descriptive answer to similar question this. It'll be useful.
I am trying to follow this as reference Serving Static content in SpringBoot
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
/**
* Created by Eric on 11/25/2015.
*/
#org.springframework.stereotype.Controller
public class Controller {
#RequestMapping("/appPage")
public String greeting(#RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
model.addAttribute("title", "Best Of the App");
model.addAttribute("basecontext", "Best Of the App");
return "appPage";
}
}
My HTML form being below
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
<input type="hidden" type="text" id="basecontext" value='${basecontext}'/>
</body>
</html>
I am trying to set value in hidden input field. But that gives me error
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="appPage", line 9 - column 33
I am trying to move this html page which was getting loaded in a JSP application to Spring Boot + ThymeLeaf.
If I simply place this content in index.html without a Context handler in Controller. the page is loaded just fine. Thymeleaf does not throw any error.
ThymeLeaf uses xml and not html and you are not allowed to have attributes of the same name in xml type="hidden" type="text"
You actually should get SAXParseException: Attribute "type" was already specified for element in your spring log
I use eclise to create a servlet like this :
package hello;
public class NewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
String name = request.getParameter("textField");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<html><head></head><body><center>");
pw.print("Hello " + name + "!");
pw.print("</center></body></html>");
}
}
and a html file like :
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="NewServlet">
<p align="center">
<font>Input some text</font> <br> <input type="text"
name="textFiled"> <br> <input type="submit"
value="submit"> <br>
</p>
</form>
</body>
</html>
when i run the servlet, met an error :
HTTP Status 404 - Servlet NewServlet is not available
--------------------------------------------------------------------------------
type Status report
message Servlet NewServlet is not available
description The requested resource (Servlet NewServlet is not available) is not available.
i checked the folder : WEB-INF or any folder else and can't see file .class
How is this caused and how can I solve it?
You should check web-inf folder in your IDE and map your servlet in web.xml file
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
make sure that this mapping is done properly and also your servlet is not in any package or folder if so then in servlet tag write that class name followed by . and your servlet name.
If problem still arises just make sure that you delete that .class file of your servlet and build your project again.(Net beans have an option of clean and build and then run) haven't use eclipse but i am sure it has similar option as well
Servlets needs to be registered and mapped on a specific URL pattern in order to be able to execute them by a HTTP request. Given your HTML code you seem to expect the servlet to listen on an URL of /NewServlet.
If you're using Tomcat 7, then just put the #WebServlet annotation on the class with exactly that URL pattern:
#WebServlet("/NewServlet")
public class NewServlet extends HttpServlet {
// ...
}
If you're still on Tomcat 6 or older for some reason, then you'd need to do it by the old fashioned web.xml way. A concrete example can be found in our servlets wiki page.
Eclipse won't show .class files in the project explorer. It will only show them in the navigator, in the /build folder. But this should not be something to worry about right now.
Format in web.xml should be like this.
<servlet-name>NewServlet</servlet-name>
<servlet-class>PackageName.JavaClass</servlet-class>
which in your case is
<servlet-name>NewServlet</servlet-name>
<servlet-class>Hello.NewServlet</servlet-class>
edite this:
form method="post" action="NewServlet"
to this
form method="post" action="/(your project name ) /NewServlet"
i had the same problem and this worked for me
My system has Tomcat seven and all the files are under webapps. The file structure is
webapps/ WelcomeForm/ web/ WelcomeForm.html
WEB-INF/ web.xml
lib/ classes/ hello/ HelloWorldServlet.java HelloWorldServlet.class
The web folder holds WelcomeForm.html.
WEB-INF holds the web.xml.
lib holds servlet-api.jar and
classes holds HelloWorldServlet.java.
the html file runs fine but I cannot run the Java file as it returns the message:
HTTP Status 404 - /hello
type Status report
message /hello
description The requested resource (/hello) is not available.
The code for the files is below:
WelcomeForm.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="POST" action="/hello">
<font size="10" color="red">Hello World!</font><BR>
Type your first name and click submit button <input TYPE=TEXT NAME="username" SIZE=20>
<P><input type="submit" value="Submit">
</form>
</body>
</html>
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">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>hello.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
HelloWorldServlet.java
package hello;
import java.io.IOException;
import javax.servlet.ServletException;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
PrintWriter out = response.getWriter();
out.write("Your Name is :" );
out.print(name);
}
}
What am I doing wrong?
Check browser URL. You are missing context.
Let Say, You are running at http://localhost:8080/test/index.jsp where test is your context path.
So, When calling Servlet It should be like http://localhost:8080/test/hello.
In your case, Its not like that.
So, Adding cotextpath will solve your problem.
e.g. action="<%=request.getContextPath()%>/hello"
OR you can use
e.g. action="hello"
So, When you add "/" to action always add context path.