HTTP Status 404 --- Spring MVC - spring

I run the code on tomcat 8.0.39 and it first shows the login.jsp When I fill the name and password and hit submit it returns the 404 error:login.jsp image
HTTP Status 404 image
My code follows:
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.ezmsip;
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;
#WebServlet(urlPatterns="/login.do")
public class LoginServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("WEB-INF/views/login.jsp").forward(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("name", request.getParameter("name"));
request.getRequestDispatcher("WEB-INF/views/welcome.jsp").forward(request, response);
}
}
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hola desde una JSP</title>
</head>
<body>
<form action="/login.do" method="post">
Nombre: <input type="text" name="name" /> Password: <input type="password" name="password" /> <input type="submit" value="Log-in" />
</form>
</body>
</html>
welcome.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bienvenido!</title>
</head>
<body>
Bienvenido! ${name}
</body>
</html>
Thanks!

It doesn't complain about login.jsp. It complains about /login.do. Did you deploy this app as the root web app in tomecat? Otherwise, it's under a context path, and you need your action to be something like /myWebApp/login.do.
In any case, the right thing to do is to prepend the context path:
action="${pageContext.request.contextPath}/login.do"
or, using the JSTL:
action="<c:url value='/login.do'/>"

Related

HTTP Status 500 - An exception occurred processing JSP page

Hi i'm creating a simple form and displaying their value in another jsp page using maven struts 1.3 dependencies but i m getting the following error
HTTP Status 500 - An exception occurred processing JSP page /success.jsp at line 13
type Exception report
message An exception occurred processing JSP page /success.jsp at line 13
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /success.jsp at line 13
10: </head>
11: <body>
12: <h1>Congrats!!!</h1>
13: <bean:write name="loginForm" property="firstName" />
14: </body>
15: </html>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "loginForm" in any scope
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.success_jsp._jspService(success_jsp.java:147)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.jsp.JspException: Cannot find bean: "loginForm" in any scope
org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:864)
org.apache.struts.taglib.bean.WriteTag.doStartTag(WriteTag.java:233)
org.apache.jsp.success_jsp._jspx_meth_bean_005fwrite_005f0(success_jsp.java:167)
org.apache.jsp.success_jsp._jspService(success_jsp.java:131)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
here is all of my code
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven</groupId>
<artifactId>StrutsWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>StrutsWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>1.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-taglib</artifactId>
<version>1.3.10</version>
</dependency>
</dependencies>
<build>
<finalName>StrutsWebApp</finalName>
</build>
</project>
web.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" 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>LoginFormStruts</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="amit.forms.LoginForm" />
</form-beans>
<action-mappings>
<action name="loginForm" path="/login"
type="amit.action.LoginAction" scope="request"
input="/login.jsp">
<forward name="success" path="/success.jsp" redirect="true" />
</action>
</action-mappings>
</struts-config>
LoginAction.java
package amit.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {
#Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("hello");
return mapping.findForward("success");
}
}
LoginForm.java
package amit.forms;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
private static final long serialVersionUID = 1L;
private String firstName = null;
private String lastName = null;
private String phoneNo = null;
private String zipCode = null;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!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 Example</title>
</head>
<body>
<html:form action="/login" focus="firstName">
<table>
<tr><td>First Name:</td><td><html:text property="firstName" /></td></tr>
<tr><td>Last Name:</td><td><html:text property="lastName" /></td></tr>
<tr><td>Phone No:</td><td><html:text property="phoneNo" /></td></tr>
<tr><td>Postal Code:</td><td><html:text property="zipCode" /></td></tr>
<tr><td colspan="2"><html:submit value="login" /></td></tr>
</table>
</html:form>
</body>
</html>
}
success.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!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>Welcome Page</title>
</head>
<body>
<h1>Congrats!!!</h1>
<bean:write name="loginForm" property="firstName" />
</body>
</html>
i just remove the
scope="request"
from my struts-config.xml and its start working

Ajax call to Dispatcher servlet

I am trying to make a simple AJAX call to dispatcher servlet from JSP and get string as response.But,I am able to call dispatcher servlet using form tag in jsp and success page is getting called.If I use ajax call is not even reaching dispatcher servlet.Please find the code for reference.I just want to know whether am I doing it in a right way.My goal is to get AJAX response
test.jsp
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs - Default functionality</title>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.jqGrid.min.js"></script>
<script>
$(document).ready(function() {
$("#btn1").click(function(){
$.ajax({
type: "Post",
url: "hello",
success: function(resp){
alert(resp)
}
});
});
});
</script>
</head>
<body>
<input type ="button" id="btn1" />
</body>
</html>
HelloController.java
package test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
#Controller
#RequestMapping("/hello")
public class HelloController{
#RequestMapping(method = RequestMethod.POST)
#ResponseBody
public String printHello(ModelMap model) {
return "success";
}
}
HelloWeb-servlet.xml
<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"
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">
<context:component-scan base-package="test" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
It doesn't matter if you're using Ajax call or not, the request should reach to the desired controller.
With that being said, and the fact you wrote that the request doesn't even reach to its destination, then your problem is within your URL.
I would recommend using the Spring Url tag which is a part of Spring tag library. It will help you build your URL within your JSP files.
In your URL example, you obviously missing the complete URL, I believe it should be: http://localhost:8080/your_project_name/hello
So just do this:
add the next directive to your JSP file:
<%# taglib uri="http://www.springframework.org/tags" prefix="spring" %>
use the spring url tag within you Ajax call:
$.ajax
({
type: "Post",
url: '<spring:url value="/hello"/>',
success: function(resp){
alert(resp)
}
});
So the final result of your JSP should be:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs - Default functionality</title>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.jqGrid.min.js"></script>
<script>
$(document).ready(function() {
$("#btn1").click(function(){
$.ajax
({
type: "Post",
url: '<spring:url value="/hello"/>',
success: function(resp){
alert(resp)
}
});
});
});
</script>
</head>
<body>
<input type ="button" id="btn1" />
</body>
</html>

Spring MVC - Uploading File Status 400 - Required MultipartFile parameter 'file' is not present

I'm new in Spring. I have excercise to make a Web app where the file is uploaded and then written to database. I'm making it with Spring MVC and Maven in Netbeans.
I made a fully working basic project based on this tutorial
https://saarapakarinen.wordpress.com/2015/01/11/building-a-basic-spring-3-web-project-netbeans-and-maven/
And tried to expand it for my application, wanted to make file uploading component based of official Spring tutorial
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multipart
but its not working, got an error:
HTTP Status 400 - Required MultipartFile parameter 'file' is not present
I've expanded this project with wyslij.jsp (form for upload file)
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wysylanie pliku</title>
</head>
<body>
<form method="get" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
And added Controller for uplading file called UpladController.java
package helloweb;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
#Controller
public class UploadController
{
#RequestMapping(value = "wyslij", method = RequestMethod.GET)
public String handleFormUpload(#RequestParam("file") MultipartFile file) throws IOException
{
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:tak";
} else
{
return "redirect:nie";
}
}
}
web.xml file:
<?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">
<!-- name of the project//-->
<display-name>HelloProject</display-name>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/application/*</url-pattern>
</servlet-mapping>
<!-- max time of the session //-->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- default page //-->
<welcome-file-list>
<welcome-file>application/wyslij.jsp</welcome-file>
</welcome-file-list>
</web-app>
and front-controller-serlvet.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- configuration to fetch jsp files automatically from WEB-INF/jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
<context:component-scan base-package="helloweb" />
</beans>
Could someone tell, why is an error and explain?
EDIT:
I decided to use form.jsp and HelloController.java made in tutorial and convert it to file upload (they were working more than my code)
form.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form Page</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="http://localhost:8084/Hello/application/form">
<label>file to send: <input type="file" name="file" /></label>
<input type="submit" />
</form>
</body>
</html>
and HelloController.java
package helloweb;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
#Controller
public class HelloController
{
#RequestMapping(value = "form", method = RequestMethod.POST)
public String login(#RequestParam(value = "file", required = true) MultipartFile file) throws IOException
{
if (!file.isEmpty())
{
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:tak";
}
else
{
return "redirect:nie";
}
}
#RequestMapping("form")
public String viewLoginPage(Model model)
{
return "form";
}
}
Now I have at least file upload form displayed properly at start page, but after selecting a file and click button, I get antoher error:
HTTP Status 500 - Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
Chnaged and Controller now is:
#Controller
public class UploadController
{
#RequestMapping(value = "wyslij", method = RequestMethod.POST)
public String handleFormUpload(#RequestParam("file") MultipartFile file) throws IOException
{
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:tak";
} else
{
return "redirect:nie";
}
}
}
and the jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wysylanie pliku</title>
</head>
<body>
<form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
tested service with postman
result for GET test:
HTTP Status 405 - Request method 'GET' not supported
type Status report
message Request method 'GET' not supported
description The specified HTTP method is not allowed for the requested resource.
result for POST test:
HTTP Status 400 - Required MultipartFile parameter 'file' is not present
type Status report
message Required MultipartFile parameter 'file' is not present
description The request sent by the client was syntactically incorrect.
You are sending file with multipart/form data encoding which needs Http POST. In your controller change it to post like below.
#RequestMapping(value = "wyslij", method = RequestMethod.POST)
And in your jsp as well.
<form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>

Unable to call modelMap in JSP

I have a Spring MVC controller which maps test to test.jsp. It has ModelMap object which has to be accessed by JSP page.
Here is controller
#RequestMapping(value = "/test")
public String test(ModelMap map)
{
map.addAttribute("data","the string which should bbe printed");
return "test";
}
Here is test.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<c:out value="${data}" />
</body>
</html>
I am getting nothing on the screen.
Please help me out.

Servlets and Spring Integration

I'm new in JSP and Servlets,
web.xml file
<?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>LoginPage</servlet-name>
<servlet-class>com.planner.servlet.LoginPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginPage</servlet-name>
<url-pattern>/LoginPage</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContextBL.xml</param-value>
</context-param>
</web-app>
index.jsp file
<%# 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 method="POST" action="/LoginPage">
<table border="1">
<tr>
<td>Login</td>
<td><input type="text" name="login" />
</td>
</tr>
<tr>
<td>Senha:</td>
<td><input type="password" name="pass" /></td>
</tr>
<tr>
<input type="submit" value="Entrar" />
</tr>
</table>
</form>
</body>
</html>
LoginPage.java
package com.planner.servlet;
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 org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Servlet implementation class LoginPage
*/
public class LoginPage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public LoginPage() {
super();
}
#Override
public void init() throws ServletException {
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();
// bf.autowireBean(this);
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title> TESTE </title>");
out.println("</head>");
out.println("<body>");
out.println("Hello World!");
out.println("</body>");
out.println("</html>");
}
}
PROBLEM:
When I click on the submit button, it shows me the error:
**type Status report
message /Apontador_Web/LoginPage
description The requested resource is not available.**
And When I remove the tags "listener" and "context-param"
it loads the servlet.
What can be happening?
go to glashfish server output and look for an some exception
Tell me the name off exception
print all
You will need to add #WebServlet(urlPatterns={"/LoginPage"}) above your class name i.e public class LoginPage extends HttpServlet {

Resources