Currently I am working on Openxava frame work and it's new for me. I want to built a File download functionality in my current project, so for that i need a HttpServletResponse object. so please help me how do i get HttpServletResponse object in Openxava.
You can create a servlet and register it in servlets.xml (OpenXava adds the content of this file to web.xml upon deployment).
To enable the servlet for the user then create an action that implements IForwardAction.
For example servlet.xml might have:
<servlet>
<servlet-name>myDownloadServlet</servlet-name>
<servlet-class>org.webapp.test.MyDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myDownloadServlet</servlet-name>
<url-pattern>/mydownload.do</url-pattern>
</servlet-mapping>
And the MyDownloadServlet class.
public class MyDownloadServlet extends HttpServlet {
/**
* Shows Hello World.
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().write("Hello World");
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Finally your action
public class MyDownloadAction extends ViewBaseAction implements IForwardAction {
public String getForwardAction() {
return "/mydownload.do";
}
public boolean inNewWindow() {
return true;
}
}
Federico
Related
I write a webapplication in java with tomcat. I write sevlets, for processing login, registration and other. This sevlets are similari. I want combine them into one, but I don't know how I can do this.
Can you give me advise or link how to fix this problem?
This is my servlets:
#WebServlet("/login")
public class LoginServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("/loginForm.jsp");
view.forward(request,response);
}
}
#WebServlet("/controller")
public class Controller extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = new User("qwert", "qwerty","qwerty",0);
req.setAttribute("User", user);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/booksUser.jsp");
requestDispatcher.forward(req,resp);
}
}
#WebServlet("/registration")
public class RegistrationServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher view = req.getRequestDispatcher("/index.jsp");
view.forward(req, resp);
}
}
I have simple Srvlet class with following Get method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello from Get method");
}
When I try to run this on Tomcat server , 8.5, I get the following message on browser : "Served at: /SimpleServletProject". This was the message which i removed from the default implementation of the Servlet.
And nothing is getting printed on Console.
Somehow my changes are not reflecting.
Try this one
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("Hello from Get method");
}
In your web.xml you should map your servlet like
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>full path of class</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/url</url-pattern>
Is it a bad or good idea to use java servlet for angular authorization?
for example:
public class LoginServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.login(....);
}
}
It depends on the requirements. For simple solution you could use it. For something more advanced you probably would need to use some framework e.g. Spring.
In the below code whenever I am starting the server or entire application the init method is suppose to be called. And the directories intended should be create if they do not exist. But in my system its not being created. And when I am writing a simple java class with main method its creating successfully(directory creation code is in main method itself). For information I am using netbeans 8.0.2 on ubuntu 14.04.
public class UserRegistration extends HttpServlet {
public void init(){
new File("Directory").mkdirs();
new File("Directory/Broker").mkdirs();
new File("Directory/User").mkdirs();
//these directories are not being created
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
}
I have run into a weird problem. My servlet's doGet method is getting called multiple times for a single HTTP request. The rerun happens every 10-12 seconds till the initial process completes.
Below is my servlet code
private static final long serialVersionUID = WebServiceServlet.class.getCanonicalName().hashCode();
private ServletContext servletContext;
/**
* #see HttpServlet#HttpServlet()
*/
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
servletContext = servletConfig.getServletContext();
}
/*public WebServiceServlet() {
super();
}*/
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String output = null;
/*
* Calling the Operation Manager which will decide the operation type
* and call the corresponding operation binder and set the return
* response generated in http response.
*/
// Request Processing
response.setContentType("application/json; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(output);
out.close();
}
#Override
public void destroy() {
super.destroy();
}
Below is the mapping in the web.xml
<servlet>
<description></description>
<display-name>WebServiceServlet</display-name>
<servlet-name>WebServiceServlet</servlet-name>
<servlet-class>com.servlet.WebServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebServiceServlet</servlet-name>
<url-pattern>/web.do</url-pattern>
</servlet-mapping>
I am using SEAM and JSF but this is a standalone servlet. There is no exception in the logs. I have also verified that the INIT method is being called only once. It is the service method which is being repeated. The identity hash code comes same for all the reruns (System.identityHashCode(this)).
The call is being made from a REST API tester. There are no multiple calls happening from the caller. The reruns are happening over the tomcat container.
I am at my wit's end. Has anyone else faced this issue?
I had faced the same issue.
Just keep the #post method in your servlet class. Comment out #get and #put if you have.
Thanks