how to write first servlet class in Eclipsed - java

package com.example;
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 Helloworld
*/
#WebServlet("/Helloworld")
public class Helloworld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Helloworld() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I have created a class hello-world and added Servelte API but I am getting Error #WebServlet("/Helloworld") and show Error Messge [The attribute value is undefined for the annotation type WebServlet].
Please tell me how to fix it?

http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html
Try to add atrribute value: #WebServlet(value="/Helloworld")

Related

Eclipse Can't Open Servlet in Browser

I just finish making my first servlet and it won't load in the web browser. I didn't choose to use the web.xml file. Here's the code:
'''
package myFirstServlet;
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 HelloWorld
*/
#WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(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());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
'''
Here's a screenshot of my project not loading. I'm tearing my hair out. What am I doing wrong?

Issue with servlet defaulting to index servlet. Tomcat 8.0.30 JDK 1.8

So I have a servlet listening on "/ajax/foo", and a servlet listening on "/". When I make a call to /ajax/foo, it initializes that servlet but doesn't call the get or post overridden methods it defaults to "/" for those, even thou I am not extending it, I am extending HttpServlet.
I am also using annotations to setup the servlet paths. I wonder if this can be causing it. I also checked all the methods and even copied over servlet doPost/doGet methods, that are working since I can verify it yet in this servlet still don't get called. Servlet doesn't write anything out, and is being called over ajax (jquery $.post).
Thank you for any help.
package ajax;
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("/ajax/foo")
public class FooAjaxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ThreadAdminActionAjaxServlet() {
super();
System.out.println("LOADED");
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("GET")
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("POST");
}
}

Best Way to output SoyTemplates from a Servlet

What is the best waya to render or output Soy Templates from closure-templates to a browser?
Currently i have the following:
package de.envisia.erp.web.servlet;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
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 com.google.template.soy.SoyFileSet;
import com.google.template.soy.tofu.SoyTofu;
/**
* Servlet implementation class EntryPoint
*/
#WebServlet("/EntryPoint")
public class EntryPoint extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public EntryPoint() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String pathContext = servletContext.getRealPath("/WEB-INF/");
//response.getWriter().println(pathContext);
SoyFileSet sfs = new SoyFileSet.Builder().add(new File(pathContext + "\\templates\\hello.soy")).build();
SoyTofu tofu = sfs.compileToTofu();
String out = tofu.newRenderer("hello.world").render();
response.getWriter().println(out);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
But i don't think it is a good practice to use the println or even print() method of the response object, are there any better ways?
Take a look at https://github.com/codedance/silken.
The basic idea is to make a special servlet and forward requests to it:
RequestDispatcher rd = getServletContext().getRequestDispatcher("/soy/products.boat.sailingBoatView");
rd.forward(req, resp);

Using <a href> to link to servlet

I have a anchor which i want it to be linked to a LogoutServlet so that it will destroy the sessions and redirect it back to a login page.
LogoutServlet.java
package pkg;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LogoutServlet
*/
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public LogoutServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
request.getSession().invalidate();
RequestDispatcher rd = request.getRequestDispatcher("Login.html");
rd.forward(request, response);
}
}
tag
Logout
Is this the right way to implement it?? I used this but it did not redirect me to Login.html .
That will hit the doGet method not the doPost method. An anchor link like that is a HTTP GET request.
If you wish to make a POST request, you will need to submit a form to the servlet using method POST.
Move your code to doGetinstead of doPost and try that.
Use doGet method. a href will use GET method.

Setting Initialization Parameters in a Servlet

I have to set initialization parameters based on environment to a servlet, NOT via web.xml but via code,
but my servlet version is not 3.0 , so I cannot use this
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#setInitParameter%28java.lang.String,%20java.lang.String%29
I dont have access to the servlet code so I am writing a new servlet that extends it and I want to add initialization parameters via java code ..Any suggestions?
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>abc</servlet-class>
<init-param>
<param-name>abc</param-name>
<param-value>localhost:2001</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Is there a way to do the above thing(adding init-params) by extending servlet abc and setting attributes to the servlet config (overriding init())?
I overrode the method getInitParameter of GenericServlet and I was able to solve my problem..
#Override
public String getInitParameter(String name) {
//Get initparams here
return "MyInitParams";
}
See if following code helps you
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorld
*/
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
ServletContext context;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
// Do required initialization
Properties prop = new Properties();
try {
prop.load(getServletContext().getResourceAsStream(
"/WEB-INF/properties/sample.properties"));
context.setAttribute("abc", prop.getProperty("abc"));
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

Categories

Resources