Eclipse Can't Open Servlet in Browser - java

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?

Related

How redirect request from jsp to a servlet

Thank you for your help in advance. I am trying to get the answer from a jsp using a radio button object and after sending to a servlet which will manage the request and send to specific servlet to impact the database. I have problems in the servlet that manages the request. I am not able to resend the petition to the final servlet. The parts of the source code are the next:
PolicyManager.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Change the current policy</h1>
<form action="ManageRequest" method="Post">
Which action would you like to apply?<br><br>
<input type="radio" name="policy" value="add">Add<br>
<input type="radio" name="policy" value="delete">Delete
<br><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
ManageRequest.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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;
/**
* Servlet implementation class ManageRequest
*/
#WebServlet("/ManageRequest")
public class ManageRequest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ManageRequest() {
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
PrintWriter printWriter = response.getWriter();
printWriter.print("doGet method");
String policy=request.getParameter("policy");
if (policy.equals("add") ) {
printWriter.println("<html><body>Redirecting to Add servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/AddPolicy");
dispatcher.forward(request, response);
} else if (policy.equals("delete")) {
printWriter.println("<html><body>Redirectin to Delete servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/DeletePolicy");
dispatcher.forward(request, response);
}
else{
printWriter.println("<html><body>A different option..."+"<br>");
}
printWriter.println("</body></html>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter printWriter = response.getWriter();
String policy=request.getParameter("policy");
printWriter.print("doPost method");
if (policy.equals("add") ) {
printWriter.println("<html><body>Redirecting to Add servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/AddPolicy");
dispatcher.forward(request, response);
} else if (policy.equals("delete")) {
printWriter.println("<html><body>Redirectin to Delete servlet section..."+"<br>");
RequestDispatcher dispatcher = request.getRequestDispatcher("/DeletePolicy");
dispatcher.forward(request, response);
}
else{
printWriter.println("<html><body>A different option..."+"<br>");
}
printWriter.println("</body></html>");
}
}
AddPolicy.java:
package PolicyManager;
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;
/**
* Servlet implementation class AddPolicy
*/
#WebServlet("/AddPolicy")
public class AddPolicy extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public AddPolicy() {
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
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Add Policy</h1>");
printWriter.println("Add Policy");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
DeletePolicy.java:
package PolicyManager;
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;
/**
* Servlet implementation class DeletePolicy
*/
#WebServlet("/DeletePolicy")
public class DeletePolicy extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public DeletePolicy() {
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
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Delete Policy</h1>");
printWriter.println("Delete Policy");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
The web.xml:
<servlet>
<servlet-name>ManageRequest</servlet-name>
<servlet-class>PolicyManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ManageRequest</servlet-name>
<url-pattern>/PolicyManager</url-pattern>
</servlet-mapping>
The problem is that only I can get the manager servlet that has to redirect the petition to the final servlet.
Thank you for your help.
Cheers
You should try to understand the difference between RequestDispatcher.include() and RequestDispatcher.forward().
RequestDispatcher.forward() means the complete control for request processing is forwarded to another servlet. The forwarding servlet should not do anything like writing response or committing response. If the response got committed in the calling servlet before the forward, then an IllegalStateException is thrown. IF the response is not yet committed, then the response will be cleared.
Whatever, it is not valid to write something to response when you are forwarding to another servlet.
If you have a requirement to do write something to response from various servlets, you should use RequestDispatcher.include().
In your case, you are writing below thing before forward() which may be the issue.
printWriter.println("<html><body>Redirecting to Add servlet section..."+"<br>
You have to add:
response.sendRedirect("http://localhost:8080/clientLibrary/DeletePolicy");
Instead of :
RequestDispatcher dispatcher = request.getRequestDispatcher("/DeletePolicy");
dispatcher.forward(request, response);
The same for AddPolicy.
Cheers

how to write first servlet class in Eclipsed

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

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