Questions tagged [servlets]
Servlet is a Java application programming interface (API) running on the server machine which can intercept the requests made by the client and can generate/send a response accordingly.
servlets
33,245
questions
1258
votes
8
answers
328k
views
How do servlets work? Instantiation, sessions, shared variables and multithreading
Suppose, I have a webserver which holds numerous servlets. For information passing among those servlets I am setting session and instance variables.
Now, if 2 or more users send request to this ...
883
votes
16
answers
456k
views
What is the difference between JSF, Servlet and JSP?
I have some questions. These are :
How are JSP and Servlet related to each other?
Is JSP some kind of Servlet?
How are JSP and JSF related to each other?
Is JSF some kind of Pre-Build UI based JSP ...
720
votes
14
answers
652k
views
How can I upload files to a server using JSP/Servlet?
How can I upload files to server using JSP/Servlet?
I tried this:
<form action="upload" method="post">
<input type="text" name="description" />
...
394
votes
12
answers
222k
views
What is Java Servlet?
I read many articles to understand Java servlet but I did not succeed.
Can you please give brief introduction of Java servlets (in easy language). What is a servlet? What are the advantages?
I can't ...
367
votes
5
answers
116k
views
Design Patterns web based applications [closed]
I am designing a simple web-based application. I am new to this web-based domain.I needed your advice regarding the design patterns like how responsibility should be distributed among Servlets, ...
357
votes
17
answers
766k
views
How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
I want to develop with Servlets in Eclipse, but it says that the package javax.servlet / jakarta.servlet cannot be resolved. How can I add javax.servlet / jakarta.servlet package to my Eclipse project?...
353
votes
8
answers
375k
views
How should I use servlets and Ajax?
Whenever I print something inside the servlet and call it by the web browser, it returns a new page containing that text. Is there a way to print the text on the current page using Ajax?
I'm very new ...
336
votes
10
answers
406k
views
Difference between getAttribute() and getParameter()
What are the differences between the getAttribute() and the getParameter() methods in HttpServletRequest?
298
votes
12
answers
344k
views
HttpServletRequest to complete URL
I have an HttpServletRequest object.
How do I get the complete and exact URL that caused this call to arrive at my servlet?
Or at least as accurately as possible, as there are perhaps things that ...
237
votes
11
answers
301k
views
Maven dependency for Servlet 3.0 API?
How can I tell Maven 2 to load the Servlet 3.0 API?
I tried:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version&...
230
votes
6
answers
276k
views
Where to place and how to read configuration resource files in servlet based application?
In my web application I have to send email to set of predefined users like [email protected], so I wish to add that to a .properties file and access it when required. Is this a correct procedure, if ...
223
votes
9
answers
64k
views
Java / Jakarta EE web development, where do I start and what skills do I need? [closed]
I want to learn, at least at a basic level, how to build Java web applications (coming from a .NET background). I would like to be able to build, deploy a simple CMS type application from the ground ...
215
votes
5
answers
228k
views
What is WEB-INF used for in a Java EE web application?
I'm working on a Java EE web application with the following source code directory structure:
src/main/java <-- multiple packages containing Java classes
src/test/java ...
205
votes
12
answers
503k
views
Get the POST request body from HttpServletRequest
I am trying to get the whole body from the HttpServletRequest object.
The code I am following looks like this:
if ( request.getMethod().equals("POST") )
{
StringBuffer sb = new StringBuffer();
...
204
votes
5
answers
247k
views
How to use a Servlet Filter to change/rewrite an incoming URL?
How can I use a Servlet Filter to change an incoming URL from
http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123
to
http://nm-java.appspot.com/Check_License?Contact_Id=My_Obj_123
?
...
193
votes
5
answers
198k
views
Difference between / and /* in servlet mapping url pattern
The familiar code:
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<...
191
votes
4
answers
119k
views
How to define servlet filter order of execution using annotations in WAR
If we define webapp specific servlet filters in WAR's own web.xml, then the order of execution of the filters will be the same as the order in which they are defined in the web.xml.
But, if we define ...
180
votes
11
answers
286k
views
What does the servlet <load-on-startup> value signify
I am getting a bit confused here. In our application we are having a few servlets defined. Here is the excerpt from the web.xml for one of the servlets:
<servlet>
<servlet-name>...
174
votes
4
answers
202k
views
What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest?
I'm making a simple, very lightweight front-controller. I need to match request paths to different handlers (actions) in order to choose the correct one.
On my local machine HttpServletRequest....
172
votes
3
answers
108k
views
Tomcat VS Jetty [closed]
I'm wondering about the downsides of each servers in respect to a production environment. Did anyone have big problems with one of the features? Performance, etc. I also quickly took a look at the new ...
170
votes
13
answers
532k
views
How do you return a JSON object from a Java Servlet
How do you return a JSON object form a Java servlet.
Previously when doing AJAX with a servlet I have returned a string. Is there a JSON object type that needs to be used, or do you just return a ...
166
votes
2
answers
445k
views
HttpServletRequest get JSON POST data [duplicate]
I am HTTP POST-ing to URL http://laptop:8080/apollo/services/rpc?cmd=execute
with
POST data
{ "jsondata" : "data" }
Http request has Content-Type of application/json; charset=UTF-...
164
votes
7
answers
500k
views
The import javax.servlet can't be resolved [duplicate]
I'm trying to use eclipse for Java EE to develop web applications.
I need to use Tomcat as my server. I've downloaded Tomcat and it's running. But my program doesn't compile.
I get the following ...
158
votes
4
answers
172k
views
HttpServletRequest - how to obtain the referring URL?
I need to log URLs that are linking to my site in a Java Servlet.
157
votes
9
answers
148k
views
Differences between cookies and sessions?
I am training in web developement and am learning about JSP & Servlets. I have some knowledge of HttpSession - I have used it in some of my sample projects.
In browsers I have seen the option to "...
152
votes
6
answers
96k
views
IntelliJ and WAR....changed files are not automatically recognized by server
I am running Intellij Ultimate with Tomcat and deploy a war. Everything deploys fine to the webapp directory of tomcat.
When I change a file like an xhtml file, is there a way for tomcat to ...
152
votes
8
answers
318k
views
How to get request URI without context path?
The Method request.getRequestURI() returns URI with context path.
For example, if the base URL of an application is http://localhost:8080/myapp/ (i.e. the context path is myapp), and I call request....
152
votes
14
answers
182k
views
Servlet for serving static content
I deploy a webapp on two different containers (Tomcat and Jetty), but their default servlets for serving the static content have a different way of handling the URL structure I want to use (details).
...
151
votes
3
answers
261k
views
How to specify the default error page in web.xml?
I am using <error-page> element in web.xml to specify the friendly error page when user encounters a certain error such as error with code of 404:
<error-page>
<error-code>...
147
votes
14
answers
151k
views
Eclipse Build Path Nesting Errors
I'm working on a simple JSP/Servlet/Tomcat webapp for my class. The professor asked us to use a folder structure that is slightly different than the default dynamic web project structure. Rather than ...
147
votes
8
answers
254k
views
How do you remove a Cookie in a Java Servlet
How do you remove a cookie in a Java servlet?
I tried this:
http://www.jguru.com/faq/view.jsp?EID=42225
EDIT: The following now works successfully it appears to be the combination of:
response....
144
votes
20
answers
285k
views
How to differ sessions in browser-tabs?
In a web-application implemented in java using JSP and Servlets; if I store information in the user session, this information is shared from all the tabs from the same browser. How to differ sessions ...
143
votes
9
answers
163k
views
RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
What is the conceptual difference between forward() and sendRedirect()?
141
votes
11
answers
169k
views
Simplest way to serve static data from outside the application server in a Java web application
I have a Java web application running on Tomcat. I want to load static images that will be shown both on the Web UI and in PDF files generated by the application. Also new images will be added and ...
139
votes
8
answers
223k
views
Can I exclude some concrete urls from <url-pattern> inside <filter-mapping>?
I want some concrete filter to be applied for all urls except for one concrete (i.e. for /* except for /specialpath).
Is there a possibility to do that?
sample code:
<filter>
<filter-...
135
votes
9
answers
380k
views
Getting request payload from POST request in Java servlet
I have a javascript library that is sending a POST request to my Java servlet, but in the doPost method, I can't seem to get the contents of the request payload. In chrome Developer Tools, all the ...
130
votes
11
answers
387k
views
Spring get current ApplicationContext
I am using Spring MVC for my web application. My beans are written in "spring-servlet.xml" file
Now I have a class MyClass and i want to access this class using spring bean
In the spring-servlet.xml ...
128
votes
3
answers
150k
views
Spring: how do I inject an HttpServletRequest into a request-scoped bean?
I'm trying to set up a request-scoped bean in Spring.
I've successfully set it up so the bean is created once per request. Now, it needs to access the HttpServletRequest object.
Since the bean is ...
127
votes
3
answers
156k
views
Recommended way to save uploaded files in a servlet application
I read here that one should not save the file in the server anyway as it is not portable, transactional and requires external parameters. However, given that I need a tmp solution for tomcat (7) and ...
126
votes
5
answers
776k
views
How to solve javax.net.ssl.SSLHandshakeException Error?
I connected with VPN to setup the inventory API to get product list and it works fine. Once I get the result from the web-service and i bind to UI. And also I integrated PayPal with my application for ...
124
votes
9
answers
207k
views
How to test my servlet using JUnit
I have created a web system using Java Servlets and now want to make JUnit testing. My dataManager is just a basic piece of code that submits it to the database. How would you test a Servlet with ...
124
votes
2
answers
104k
views
Why do Java webapps use .do extension? Where did it come from?
I have always wondered why so many Java developers use ".do" as the extension for their web controller (MVC) resources. Example: http://example.com/register.do
It doesn't even seem to be framework ...
121
votes
3
answers
54k
views
ContextLoaderListener or not?
A standard spring web application (created by Roo or "Spring MVC Project" Template) create a web.xml with ContextLoaderListener and DispatcherServlet. Why do they not only use the DispatcherServlet ...
118
votes
19
answers
251k
views
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
I have an HTML form in a JSP file in my WebContent/jsps folder. I have a servlet class servlet.java in my default package in src folder. In my web.xml it is mapped as /servlet.
I have tried several ...
116
votes
9
answers
417k
views
How to use Session attributes in Spring-mvc
Could you help me write spring mvc style analog of this code?
session.setAttribute("name","value");
And how to add an element that is annotated by @ModelAttribute annotation to session and then get ...
111
votes
5
answers
493k
views
doGet and doPost in Servlets
I've developed an HTML page that sends information to a Servlet. In the Servlet, I am using the methods doGet() and doPost():
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ...
110
votes
9
answers
320k
views
java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed
This method throws
java.lang.IllegalStateException: Cannot forward after response has been committed
and I am unable to spot the problem. Any help?
int noOfRows = Integer.parseInt(request....
110
votes
13
answers
157k
views
Http Servlet request lose params from POST body after read it once
I'm trying to access two http request parameters in a Java Servlet filter, nothing new here, but was surprised to find that the parameters have already been consumed! Because of this, they are not ...
109
votes
14
answers
107k
views
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
I'm writing a Spring MVC application deployed on Tomcat. See the following minimal, complete, and verifiable example
public class Application extends ...
107
votes
5
answers
100k
views
what is the difference between a portlet and a servlet?
I have been asked to work on portlets and portals, and I want to know the differences between a portlet and a servlet.
How and where does a portlet differ (maybe functionally) from a servlet?