Issues configuring web.xml mapping - servlets

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

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.

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>

Class Not Found Exception with description: The server encountered an internal error () that prevented it from fulfilling this request

Error in console:
Dec 13, 2015 12:45:54 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet LoginController
java.lang.ClassNotFoundException: flexController.LoginController
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1062)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:813)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
my servlet is:
package flexController;
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 LoginController
*/
#SuppressWarnings("serial")
#WebServlet("/LoginController")
public class LoginController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String un=request.getParameter("username");
String pw=request.getParameter("password");
if(un.equals("admin") && pw.equals("admin"))
{
response.sendRedirect("home.jsp");
return;
}
else
{
response.sendRedirect("error.jsp");
return;
}
}
}
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" 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_3_0.xsd" id="WebApp_ID" version="3.0"
metadata-complete="true">
<display-name>Flex1.1</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>flexController.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>
</web-app>
i'm writing just a simple login page using servlet, which will redirect to succes page or error page after login.

Getting HTTP Status 405 - HTTP method GET is not supported by this URL

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfig1 extends HttpServlet {
#Override
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException ,IOException{
PrintWriter pw = res.getWriter();
ServletConfig cfg = getServletConfig();
String myname = cfg.getInitParameter("myname");
pw.print("my name is"+myname);
System.out.println("hello");
}
}
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" 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>ServletConfig</display-name>
<servlet>
<servlet-name> ServletConfig1</servlet-name>
<servlet-class>ServletConfig1</servlet-class>
<init-param>
<param-name>myname</param-name>
<param-value>saurabh</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfig1</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
</web-app>
while running this servletconfig example i am getting http get method is not supported even though i have used post method in my application.what could be the reason?
change the public void doPost(HttpServletRequest req,HttpServletResponse res) to public void doGet(HttpServletRequest req,HttpServletResponse res).

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