Java servlet for upload image [duplicate] - java

This question already has an answer here:
Upload image from android to java servlet and save it
(1 answer)
Closed 9 years ago.
I would like to create a Java Servlet to allow an Android device to post photos. Where should I begin? Should I use the Apache common fileupload library?
I know how to create a servlet, basically like this right:
#WebServlet(name = "UploadServlet")
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
(I'm new to Java and web development.)

It seems you're using Servlet API 3.0, so there is a dedicated annotation for tagging methods which manage file upload: javax.servlet.annotation.MultipartConfig.
A good tutorial / example is How to write upload file servlet with Servlet 3.0 API.

Related

Same header.jsp for different web application

I have a running web application with a functional header with js and css of it's own. Now I m trying to use the same header with all the features in my another application.
Instead of duplicating the code, I need to have some kind of a controller or servlet that can provide me this JSP in proper functional format.
Currently I have tried this approach mentioned below code in a servlet :
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
CharArrayWriterResponse customResponse = new CharArrayWriterResponse(resp);
req.getRequestDispatcher("/WEB-INF/jsp/layout/header_new.jsp").forward(req, customResponse);
resp.getWriter().print(customResponse.getOutput());
}
I m getting the response as html but stil not able to use it in another application using tag.
Any suggestions would be helpful.

JSP, Servlets - proper way of managing website layout?

I am quite new to JSP, servlets, etc.
I am making a website for learning purposes. I would like it to have a classic layout - menu on the left, and content in central part. I came across problem: I want to make "dynamic menu". It would load some data from database (countries). I can do it on single page by creating a servlet that returns those countries for me.
#WebServlet("/countries")
public class LeagueCountriesServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req,resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req,resp);
}
void processRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
LeaguesDAO dao =
(LeaguesDAO)req.getServletContext().getAttribute("leaguesDAO");
List<String> list = dao.getAllCountries();
req.setAttribute("countrylist", list);
RequestDispatcher view = req.getRequestDispatcher("menulist.jsp");
view.forward(req, resp);
}
}
However, in fact it works only if I access "/countries". I would like to make it appear on many pages.
I tried to solve it with including it in other JSPs
<jsp:include page="/countries" />
But this causes that website is being "cut" after that included part.
What is the proper way to handle "layouts" like that?
I could generate response for this menu in every servlet, but it doesn't sound like a proper method.
I believe that i should divide template into two parts: pre-content holding header, menu, etc, and post-content holding footer etc, and include them in .jsp's used to display content-data coming from servlets. But how to avoid problems with dynamically generated menus?
<jsp:include page="before-content.jsp" />
(here some code to display specified web page)
<jsp:include page="after-content.jsp" />
Thanks for help!
You should change path in #WebServlet("/countries") not in JSPs, web server (Tomcat/Jetty/etc) are dispatches http requests looking into servlet mapping. Afterwards your servlet points which jsp should be used to generate desired html page.
Have a look at Apache Tiles, implementing the Composite View Pattern or Sitemesh, which uses the Decorator Pattern.
In Tiles you can define some templates for the header, footer and menu part and reuse them for every page.

How can i implement java Rest with out Jersy

Hello i am trying to develop a rest api .it have no need for performance issue or such complex design just two api . how can i develop it with out jersy using jetty server ??
Isn't there any way we can make a RESTful web service without using jersey or for that matter any other light weight libs ?
is there any Reasons for not directly write Servlets for creating a REST API ??
Here is the code for skeleton servlet. If you have issues making it run, let me know and I'll post complete sample project.
public class TestServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String query = request.getQueryString();
writer.print("Hello. You said: " + query);
}
#Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
REST is basically a concept over applied the HTTP protocol. You can implement it with Servlets and JSP, even thought it will be much harder to understand in a more complex scenario, when a base resource invokes sub-resource, building a chain call.
I would recommend that you stick to the JAX-RS specification for java REST services. It is very lightweight and easy to understand.

Servlet : doGet and doPost [duplicate]

This question already has answers here:
doGet and doPost in Servlets
(5 answers)
Closed 6 years ago.
I have 2 parameters, User and Pass. I want to send them to a servlet.
If I use the doGet method of the servlet it would look like this :
"link?User="+TextFieldValue+"&pass"textFieldValue
user= UserName.getValue();
pass= password.getValue();
Resource newPictureResource = new ExternalResource("http://localhost:8888/PieChart?UserName="+name+"&Password="+pass);
Success.setSource(newPictureResource);
editContent.addComponent(Success);
send them to servlet :
String UserName = request.getParameter("UserName");
String Password = request.getParameter("Password");
It works (tested)
If the Username + pass are right then he get a "success" picture posted on the screen.
But nobody passes the parameters via URL.
My question: How do I send the parameters using doPost method of the servlet ?
info : im working with eclipse on a liferay portal with a Vaadin portlet.
You don't send parameters in doPost(..). You receive them there. HTTP has many methods, two of which are GET and POST. It is up to the client-side to choose which method to use. POST is most often used with html forms - <form method="POST".
Vaadin should be able to send POST requests as well - see this thread
Not sure how Vaadin interacts, but typically portlet requests are handled differently. Looking though The Book of Vaadin - Portal Integration sheds some insights on configuration and action processing.
If you're looking for a way to handle both request types without reusing logic, simply choose your method of submission by either post or get from your application interface:
#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);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Enter logic here
}

Printing Request debug information on a Java Servlet

I find it extremely useful when doing Django/Python web development to fully inspect a HTTP request like this:
logger = logging.getLogger(__name__)
def index(request):
logger.info(request)
Is there something similar for a Java Servlet?
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// debugInfo should print something similar to what you get in DJANGO
debugInfo(request);
}
Since I got no answers, I wrote a little helper class to help me with this issue. Hope someone else finds it interesting and useful.

Categories

Resources