Using <a href> to link to servlet - java

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.

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

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

Categories

Resources