Servlet not working on WebSphere 8.x - servlets

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.

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>

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).

Init method not firing JSP Servlet

I am using Eclipse IDE, a simple HelloServlet.java file and a simple index.jsp file. When I run the local server, the program starts but the following code does not execute:
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("Init Firing: ");
}
I have the Console tab open, and the last statement I receive is: INFO: Server startup in 1442 ms. What might I do to get the init method to fire?
The container will only call the init() method of the servlet when it's called, not on the container startup.
If you want to start things on container startup, you can use the ContextListener as suggested here call method on server startup
This code works for me
package mine;
import java.io.IOException;
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 MySL extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
System.out.println("xyz="+config.getInitParameter("xyz"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
}
}
and 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>jsp</display-name>
<servlet>
<servlet-name>mySL</servlet-name>
<servlet-class>mine.MySL</servlet-class>
<init-param>
<param-name>xyz</param-name>
<param-value>123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mySL</servlet-name>
<url-pattern>/MySL</url-pattern>
</servlet-mapping>
</web-app>
When the server starts, nothing happens, because the init() method is called when the servlet is called.
On the first servlet call (e.g. opening in your browser something like http://myserver.mydomain:8080/myapp/MySL), you'll get
xyz=123
doGet
On the second servlet call, you'll get
doGet
Please notice that this is the "old way" of declaring things. Nowadays, Servlets configuration can be made using annotations. Careful to not mix annotations with XML declarations for the same servlet.
Servlets with annotations look like this
package mine;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(urlPatterns = { "/OtherSL" }, initParams = { #WebInitParam(name = "abc", value = "456", description = "some parameter") })
public class OtherSL extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
System.out.println("abc=" + config.getInitParameter("abc"));
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
}
}

Resources