Not able to create directory in init method of httpServlet - java

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

Related

How processing all request inside one servlet?

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

Multi-Release-War for tomcat8 on jdk10

I just read about Multi-Release-Jars what looks like a good advantage. I can not get it working using a Tomcat8 on JDK10 and not getting a stacktrace.
This is my servlet (placed at webapp.jar!/META-INF/versions/10/de/NanoNanoServlet.class):
package de;
public class NanoNanoServlet extends HttpServlet {
#SuppressWarnings("deprecation")
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("10");
}
}
This is my servlet (placed at webapp.jar!/de/NanoNanoServlet.class):
package de;
public class NanoNanoServlet extends HttpServlet {
#SuppressWarnings("deprecation")
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Other");
}
}
What I get is the console-output:
Other
But it must be
10
Using a Tomcat on JDK10

JAVA - Is there possible pass a servlet value received to a .java file, but in another project?

good afternoon. I have a .JSP file that send a value to a servlet file. I have another project, where there is a java file in that i want to get this servlet value received by .JSP file.
My question is if is there possible pass this servlet value to this .java file in another project ? Or can i send by .JSP file directly ?
I created a Java Class in my Web Application, but now, how can i pass the servlet parameter to this java class ?
I will put the code down here:
Servlet code:
public class ServletJava extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
/*I WANT TO INSTANCE THE PARAMETER HERE TO SEND TO OTHER CLASS*/
}
#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";
}
}
The Class java:
public class RecebeServlet {
public static void main(String[] args) throws Exception {
/*I WANT RECEIVE THE PARAMETER HERE !! */
}
}
It is probably bad practice to have a class with only one main mathod. I am sure that you can divide the logic from that class into several methods instead of having one giant main.
But anyway, since you asked: Just call your main method as you would call any static method:
String[] args = new String[1];
args[0] = yourValue;
RecebeServlet.main(args);
But again, please consider refactoring your RecebeServlet class. You do not want to have program logic in your main method.

Login servlet for angular authorization

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.

How do i get a HttpServletResponse object in Openxava

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

Categories

Resources