servlets

Retrieve the data sent as JSON from JavaScript, inside a servlet

∥☆過路亽.° 提交于 2019-12-29 05:35:53
问题 I have a query on retrieving data sent as JSON from a JavaScript, inside a Java servlet. Following is what I am doing... This is the part of the code inside JavaScript making a request to a servlet type : "POST", url: 'getInitialData', datatype: 'json', data : ({items :[{ name: "John", time: "2pm" },{name: "Sam", time: "1pm" }]}), success: function(data) { try{ ////Code to be handeled where response is recieved }catch(e){ alert(e); } } On making this request I try to retrieve the parameters

How to apply a servlet filter only to requests with HTTP POST method

三世轮回 提交于 2019-12-29 05:01:19
问题 In my application I want to apply a filter, but I don't want all the requests to have to go to that filter. It will be a performance issue, because already we have some other filters. I want my filter to apply only for HTTP POST requests. Is there any way? 回答1: There is no readily available feature for this. A Filter has no overhead in applying to all HTTP methods. But, if you have some logic inside the Filter code which has overhead, you should not be applying that logic to unwanted HTTP

Sending redirect to another servlet/JSP without loosing the request parameters.

和自甴很熟 提交于 2019-12-29 04:20:30
问题 How do i specify a redirection to another servlet, in the doPost() method of a servlet. at the moment I'm using request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); which is wrong since, my parameters in the doGet() method of products are not being called and initialized. So I'm left with an empty products page after logging in :/ 回答1: You need to use HttpServletResponse#sendRedirect() to send a redirect. Assuming that the servlet is mapped on an URL pattern of

Confusion about how java web session handeling works. Demystifying Cookies and Header differences using servlet api and HttpSession object

放肆的年华 提交于 2019-12-29 04:04:05
问题 I am learning Spring security and Spring MVC, but I realized I needed to learn jsp Servlets first and general web programming in a java environment. I have confusions surrounding the HttpServletRequest and HttpServletResponse objects and how they can be used to add headers to the request and response objects and how they relate to sessions. As far as I understand, a cookie is a type of header just like Content-type and Accept. The java servlet api just makes it easy to work with the header by

How to check if session exists or not?

两盒软妹~` 提交于 2019-12-29 03:27:27
问题 I am creating the session using HttpSession session = request.getSession(); Before creating session I want to check if it exists or not. How would I do this? 回答1: If you want to check this before creating, then do so: HttpSession session = request.getSession(false); if (session == null) { // Not created yet. Now do so yourself. session = request.getSession(); } else { // Already created. } If you don't care about checking this after creating, then you can also do so: HttpSession session =

How to convert String to Json

末鹿安然 提交于 2019-12-29 01:33:06
问题 I have a servlet in Java and I would like to know how I can do the following. I have a String variable with the value of a name and want to create a Json with the variable being something like {"name": "David"} . How do I do this? I have the following code but I get an error : Serious: Servlet.service () for servlet threw exception servlet.UsuarioServlet java.lang.NullPointerException at servlet.UsuarioServlet.doPost (UsuarioServlet.java: 166): at line String myString = new JSONObject().put(

Wrap the default servlet but override the default webapp path

前提是你 提交于 2019-12-28 18:44:49
问题 I have a folder of static html,imgs,flash content that lives outside of the webapp folder. Right now I'm using a symbolic link to map that folder into my webapp directory. The problem i have is when I undeploy my application it follows the symbolic link and deletes all these files. One of the solutions I'm trying to implement is a special servlet that wraps the default servlet but uses a different relative path. I'm having trouble finding out how to wrap the default servlet in a way that

How to upload an image using JSP -Servlet and EJB 3.0 [duplicate]

做~自己de王妃 提交于 2019-12-28 16:18:25
问题 This question already has answers here : How to upload files to server using JSP/Servlet? (12 answers) Closed 4 years ago . I want to upload an Image using JSP Servlet and ejb 3.0 回答1: To start, to select a file for upload using JSP you need at least a HTML <input type="file"> element which will display a file browse field. As stated in the HTML forms spec you need to set the request method to POST and the request encoding to multipart/form-data in the parent <form> element. <form action=

How do I access a text file from within my war

有些话、适合烂在心里 提交于 2019-12-28 16:13:02
问题 How do I know what file reference to use to get a file from my WAR. The structure of the WAR is: WAR src - model - web build WebContent META-INF WEB-INF LIB The JSPs are under WebContent, I have put the config.txt file under the WebContent folder and tried to get to it with BufferedReader in = new BufferedReader(new FileReader("WebContent/config.txt")); But this doesn't work. Does anyone know what reference I need to pass or how I can figure it out. 回答1: Check out ServletContext

How do I access a text file from within my war

只愿长相守 提交于 2019-12-28 16:12:04
问题 How do I know what file reference to use to get a file from my WAR. The structure of the WAR is: WAR src - model - web build WebContent META-INF WEB-INF LIB The JSPs are under WebContent, I have put the config.txt file under the WebContent folder and tried to get to it with BufferedReader in = new BufferedReader(new FileReader("WebContent/config.txt")); But this doesn't work. Does anyone know what reference I need to pass or how I can figure it out. 回答1: Check out ServletContext