Servlet Mappings - servlets

My problem is i mapped my servlets on web.xml. When i submit info to servlet it gives me "resuorces is not found" error. How can i solve this problem. Thanks your answers.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<form action="Login" method="POST">
UserName:<input type="text" name="user_name">
<BR>
Password:<input type="password" name="password">
<BR>
<input type="submit" value="Gonder">
<BR>
<input type="reset" value="Reset">
</form>
</body>
</html>
SERVLET CODES are like this
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
private String usr_name;
private String pass;
static Logger log=Logger.getLogger(Login.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PropertyConfigurator.configure("C:\\Users\\AliArdaOrhan\\workspace2\\QuizProject\\WebContent\\WEB-INF\\log4j.properties");
usr_name=request.getParameter("user_name");
pass=request.getParameter("Password");
try {
if(Validation.validate(usr_name,pass)){
RequestDispatcher rs=request.getRequestDispatcher("Welcome");
rs.forward(request, response);
}
else{
RequestDispatcher rs=request.getRequestDispatcher("login");
rs.forward(request, response);
}
} catch (ClassNotFoundException | SQLException e) {
log.error("Cannot Validate User Information", e);
}
}
}
WEB XML CODES are like this
<?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" 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>QuizProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>Login Page</description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Welcome</display-name>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>

You need to add a servlet mapping for Login action in your web.xml:
<servlet>
<description></description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
And make sure you restart your server after you make those changes in web.xml.
And if you are using Servlet 3.0 then you can use annotations like below in your servlet which are equivalent to above code:
#WebServlet("/Login")
public class Login extends HttpServlet {

Please change on this line
<form action="Login" method="POST">
replace "Login" with "/GuestBook"

You are sending your form to Login (relative URL), but your servlet awaits at /GuestBook. Change the servlet mapping or form's action attribute.

Related

Servel init-param returns null

I'm trying to load some init-param values into a serevlet through the web.xml file but they keep showing up null. I do have two context-param's that work fine. Does anyone know what I'm doing wrong?
This is the web.xml file that I'am using for my application.
<?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_3_1.xsd"
version="3.1"
>
<display-name>Hello World Application</display-name>
<servlet>
<servlet-name>contextxParameterServlet</servlet-name>
<servlet-class>com.wrox.HelloServlet</servlet-class>
<init-param>
<param-name>database</param-name>
<param-value>CustomerSupport</param-value>
</init-param>
<init-param>
<param-name>server</param-name>
<param-value>10.0.12.5</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>contextxParameterServlet</servlet-name>
<url-pattern>/contextParameters</url-pattern>
</servlet-mapping>
<context-param>
<param-name>settingOne</param-name>
<param-value>foo</param-value>
</context-param>
<context-param>
<param-name>settingTwo</param-name>
<param-value>bar</param-value>
</context-param>
<servlet>
<servlet-name>servletParameterServlet</servlet-name>
<servlet-class>com.wrox.initParams</servlet-class>
<init-param>
<param-name>database</param-name>
<param-value>CustomerSupport</param-value>
</init-param>
<init-param>
<param-name>server</param-name>
<param-value>10.0.12.5</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletParameterServlet</servlet-name>
<url-pattern>/servletParameter</url-pattern>
</servlet-mapping>
</web-app>
This is page mapped to servletParameterServlet.
package com.wrox;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;
public class initParams extends HttpServlet{
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletContext c = this.getServletContext();
PrintWriter writer = response.getWriter();
Enumeration<String> temp = c.getInitParameterNames();
while(temp.hasMoreElements()) {
writer.append(temp.nextElement());
}
writer.append("database: ").append(c.getInitParameter("database"))
.append(", server: ").append(c.getInitParameter("server"));
}
#Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
System.out.println("Servlet " + this.getServletName() + " has started.");
}
#Override
public void destroy() {
System.out.println("Servlet " + this.getServletName() + " has stopped.");
}
}
The way you have initialized things, I think what you are looking for should be available in
this.getServletConfig().getInitParameter("foo");
Broadly speaking the servlet context is shared by servlets inside a JVM but this object is present inside servlet config for a particular servlet, while <init-param> go in ServletConfig.
For more details I would strongly suggest to read the docs.

Issues configuring web.xml mapping

I have just picked up web development and I cannot for the life of me figure out what I'm doing wrong when mapping the SimpleServlet in web.xml
these are my files:
My web.xml is:
<?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"
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>JavaHelloWorldApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>wasdev.sample.servlet.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/SimpleServlet</url-pattern>
</servlet-mapping>
</web-app>
Simple Servlet
package wasdev.sample.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 SimpleServlet
*/
#WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().print("Hello World!");
}
}
I am simply trying to get a local host running and access the JavaHelloWorldApp/SimpleServlet

getting tomcat error with servlet-mapping [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.
Actually this is my first servlet application.just getting started..
below is my code:
package newpackage.org;
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;
//#WebServlet(description = "a general servlet", urlPatterns = {
"/simpleservlet" })
public class simpleservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServletdoGet(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());
PrintWriter obj= response.getWriter();;
// String username= request.getParameter("username");
obj.println("again getting back with ");
}
}`
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>myproject</display-name>
<servlet-mapping>
<servlet-name>xmlservlet</servlet-name>
<url-pattern>/002path</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>xmlservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simpleservlet</servlet-name>
<url-pattern>/001path</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
</web-app>
ERROR:
HTTP Status 404 - /myproject/servlet/newpackage.org.simpleservlet
type Status report
message /myproject/servlet/newpackage.org.simpleservlet
description The requested resource is not available.
Apache Tomcat/7.0.72
You need to remove the below in web.xml
<servlet-mapping>
<servlet-name>simpleservlet</servlet-name>
<url-pattern>/001path</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
And use the below code in servlet:
#WebServlet(description = "a general servlet", urlPatterns = {"/simpleservlet", "/001path" })
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
Give simpleservlet class name instead of xmlservlet
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.simpleservlet </servlet-class>
</servlet>

Spring MVC - Mapping to .htm doesn't work

I want to crate a simple hello world following a tutorial step by step, but I think that missing something, because my code returns just a 404
Please, can you help me with what is wrong?
This is the Controller class
package com.companyname.springapp;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by cota on 22/12/15.
*/
#Controller
public class HelloController{
protected final Log logger = LogFactory.getLog(getClass());
#RequestMapping(value="/hello.htm",method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("views/hello.jsp", "now", now);
}
}
This is the 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"
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">
<display-name>Springapp</display-name>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
This is the app-config.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Configures the #Controller programming model -->
<mvc:annotation-driven/>
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="com.companyname.springapp.web" />
</beans>
This is a header file to include all the JSP files
<%# page session="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
And the index.jsp and hello.jsp have the next line
<%# include file="views/include.jsp" %>
It's my first question here and really admire all of you, sorry for my poor english!
I don`t think so you have not properly configured view resolver. so, please try the below links. It may be work for you.
Open your app-config.xml and add the below lines.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
and modify the your controller as below.
#RequestMapping(value="/hello.htm",method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
I hope this will work for you..

Servlet not working on WebSphere 8.x

I have the following class...
package org.me.test
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("/servlet")
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/plain");
resp.getWriter().write("Test Servlet");
super.doGet(req, resp);
}
}
and the following Web.xml...
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<display-name>srv</display-name>
<servlet-name>srv</servlet-name>
<servlet-class>
org.me.test.TestServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>srv</servlet-name>
<url-pattern>/srv/*</url-pattern>
</servlet-mapping>
</web-app>
but I get...
Error 405: HTTP method GET is not supported by this URL
You are getting the 405 error because you are calling super.doGet() at the end of your doGet(). Try removing that line.
The default implementation of doGet() in the base class HttpServlet returns that 405 error. To support the GET operation, you must override doGet() without calling the parent implementation, otherwise it returns the same error intended to be displayed when there is no override.

Resources