What is Java Servlet? - java

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 understand the difference between server-side programming languages (PHP, ASP) and servlets.

A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request. Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide.
Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.

A servlet at its very core is a java class; which can handle HTTP requests.
Typically the internal nitty-gritty of reading a HTTP request and response over the wire is taken care of by the containers like Tomcat. This is done so that as a server side developer you can focus on what to do with the HTTP request and responses and not bother about dealing with code that deals with networking etc. The container will take care of things like wrapping the whole thing in a HTTP response object and send it over to the client (say a browser).
Now the next logical question to ask is who decides what is a container supposed to do? And the answer is; In Java world at least It is guided (note I did not use the word controlled) by specifications. For example Servlet specifications (See resource 2) dictates what a servlet must be able to do. So if you can write an implementation for the specification, congratulations you just created a container (Technically containers like Tomcat also implement other specifications and do tricky stuff like custom class loaders etc but you get the idea).
Assuming you have a container, your servlets are now java classes whose lifecycle will be maintained by the container but their reaction to incoming HTTP requests will be decided by you. You do that by writing what-you-want-to-do in the pre-defined methods like init(), doGet(), doPost() etc. Look at Resource 3.
Here is a fun exercise for you. Create a simple servlet like in Resource 3 and write a few System.out.println() statements in it's constructor method (Yes you can have a constructor of a servlet), init(), doGet(), doPost() methods and run the servlet in tomcat. See the console logs and tomcat logs.
Resources
Look how the HTTP servlet looks here(Tomcat example).
Servlet Specification.
Simple Servlet example.
Start reading the book online/PDF
It also provides you download of the whole book. May be this will help.
if you are just starting servlets may be it's a good idea to read the material along with the servlet API. it's a slower process of learning, but is way more helpful in getting the basics clear.

In addition to the above, and just to point out the bleedin' obvious...
To many this is hyper obvious, but to someone used to writing apps which are just run and then end: a servlet spends most of its time hanging around doing nothing... waiting to be sent something, a request, and then responding to it. For this reason a servlet has a lifetime: it is initalised and then waits around, responding to anything thrown at it, and is then destroyed. Which implies that it has to be created (and later destroyed) by something else (a framework), that it runs in its own thread or process, and that it does nothing unless asked to. And also that, by some means or other, a mechanism must be implemented whereby this "entity" can "listen" for requests.
I suggest that reading about threads, processes and sockets will throw some light on this: it's quite different to the way a basic "hello world" app functions.
It could be argued that the term "server" or "servlet" is a bit of an overkill. A more rational and simpler name might be "responder". The reason for the choice of the term "server" is historical: the first such arrangements were "file servers", where multiple user/client terminals would ask for a specific file from a central machine, and this file would then be "served up" like a book or a plate of fish and chips.

What is a Servlet?
A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request.
Basically servlets are usually used to implement web applications - but there are also various frameworks which operate on top of servlets (e.g. Struts) to give a higher-level abstraction than the "here's an HTTP request, write to this HTTP response" level which servlets provide.
Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc). One of the best-known open source servlet containers is Tomcat.
In a request/response paradigm, a web server can serve only static pages to the client
To serve dynamic pages, a we require Servlets.
Servlet is nothing but a Java program
This Java program doesn’t have a main method. It only has some callback methods.
How does the web server communicate to the servlet? Via container or Servlet engine.
Servlet lives and dies within a web container.
Web container is responsible for invoking methods in a servlets. It knows what callback methods the Servlet has.
Flow of Request
Client sends HTTP request to Web server
Web server forwards that HTTP request to web container.
Since Servlet can not understand HTTP, its a Java program, it only understands objects, so web container converts that request into valid request object
Web container spins a thread for each request
All the business logic goes inside doGet() or doPost() callback methods inside the servlets
Servlet builds a Java response object and sends it to the container. It converts that to HTTP response again to send it to the client
How does the Container know which Servlet client has requested for?
There’s a file called web.xml
This is the master file for a web container
You have information about servlet in this file-
servlets
Servlet-name
Servlet-class
servlet-mappings- the path like /Login or /Notifications is mapped here in
Servlet-name
url-pattern
and so on
Every servlet in the web app should have an entry into this file
So this lookup happens like- url-pattern -> servlet-name -> servlet-class
How to "install" Servlets?
* Well, the servlet objects are inherited from the library- javax.servlet.* . Tomcat and Spring can be used to utilize these objects to fit the use case.
Ref- Watch this on 1.5x- https://www.youtube.com/watch?v=tkFRGdUgCsE . This has an awesome explanation.

Servlet is server side technology which is used to create dynamic web page in web application. Actually servlet is an api which consist of group of classes and interfaces, which has some functionality. When we use Servlet API we can use predefined functionality of servlet classes and interfaces.
Lifecycle of Servlet:
Web container maintains the lifecycle of servlet instance.
1 . Servlet class loaded
2 . Servlet instance created
3 . init() method is invoked
4 . service() method invoked
5 . destroy() method invoked
When request raise by client(browser) then web-container checks whether the servlet is running or not if yes then it invoke the service() method and give the response to browser..
When servlet is not running then web-container follow the following steps..
1. classloader load the servlet class
2. Instantiates the servlet
3. Initializes the servlet
4.invoke the service() method
after serving the request web-container wait for specific time, in this time if request comes then it call only service() method otherwise it call destroy() method..

If you are beginner, I think this tutorial may give basic idea about What Servlet is ...
Some valuable points are below from the given link.
Servlet technology is used to create web application which resides at server side and generates dynamic web page.
Servlet can be described in many ways, depending on the context.
Servlet is a technology i.e. used to create web application.
Servlet is an API that provides many interfaces and classes including
documentations.
Servlet is an interface that must be implemented for creating any
servlet.
Servlet is a class that extend the capabilities of the servers and
respond to the incoming request. It can respond to any type of
requests.
Servlet is a web component that is deployed on the server to create
dynamic web page.
Reference:Here.

Servlets are Java classes that run certain functions when a website user requests a URL from a server. These functions can complete tasks like saving data to a database, executing logic, and returning information (like JSON data) needed to load a page.
Most Java programs use a main() method that executes code when the program in run. Java servlets contain doGet() and doPost() methods that act just like the main() method. These functions are executed when the user makes a GET or POST request to the URL mapped to that servlet. So the user can load a page for a GET request, or store data from a POST request.
When the user sends a GET or POST request, the server reads the #WebServlet at the top of each servlet class in your directory to decide which servlet class to call. For example, let's say you have a ChatBox class and there's this at the top:
#WebServlet("/chat")
public class ChatBox extends HttpServlet {
When a user requests the /chat URL, your ChatBox class with be executed.

Java Servlets are server-side Java program modules that procedure and answer customer demands and actualize the servlet interface. It helps in improving Web server usefulness with negligible overhead, upkeep and support.
A servlet goes about as a mediator between the customer and the server. As servlet modules keep running on the server, they can get and react to demands made by the customer. Demand and reaction objects of the servlet offer a helpful method to deal with HTTP asks for and send content information back to the customer.
Since a servlet is coordinated with the Java dialect, it additionally has all the Java highlights, for example, high movability, stage autonomy, security and Java database availability.

Servlet is a java class to respond a HTTP request and produce a HTTP response...... when we make a page with the use of HTML then it would be a static page so to make it dynamic we use SERVLET {in simple words one can understand}
To make use of servlet is overcomed by JSP it uses the code and HTML tag both in itself..

As this article describes, a Servlet is a standardized way of extending a Java server, and accessing its capabilities.
Each Servlet can be seen as a tiny server (hence the name), that gets access to the request and response modelled in Java code, along with other context data, like the Session.
With these in hand, the Java code of the servlet can interface with whatever it needs to to render a response, including handing off to a JSP page for generating a HTML view.

I think servlet is basically a java class which acts as a middle way between HTTP request and HTTP response.Servlet is also used to make your web page dynamic. Suppose for example if you want to redirect to another web page on server then you have to use servlets. Another important thing is that servlet can run on localhost as well as a web browser.

You just got the answer for a normally servlet. However, I want to share you about something about Servlet 3.0
What is first a Servlet?
A servlet is a Web component that is managed by a container and
generates dynamic content. Servlets are Java classes that are compiled
to byte code that can be loaded dynamically into and run by a Java
technology-enabled Web server or Servlet container.
Servlet 3.0 is an update to the existing Servlet 2.5 specification.
Servlet 3.0 required API of the Java Platform, Enterprise Edition 6.
Servlet 3.0 is focussed on extensibility and web framework
pluggability. Servlet 3.0 bring you up some extensions such as Ease of
Development (EoD), Pluggability, Async Support and Security
Enhancements
Ease of Development
You can declare Servlets, Filter, Listeners, Init Params, and almost
everything can be configured by using annotations
Pluggability
You can create a sub-project or a module with a web-fragment.xml. It
means that it allows to implement pluggable functional requirements
independently.
Async Support
Servlet 3.0 provides the ability of asynchronous processing, for
example: Waiting for a resource to become available, Generating
response asynchronously.
Security Enhancements
Support for the authenticate, login and logout servlet security
methods
I found it from Java Servlet Tutorial

Related

How can I see what Tomcat is doing with respect to Servlets and the network side

Currently, I am learning about Java web development.
A lot of it seems to simply be configuration to me, and I feel that my understanding is superficial because I only see the configuration (i.e. define your servlets and their mappings in the web.xml file, make custom Servlets by extending the HttpServlet class, instantiate Tomcat in the main method, etc.)
I want do know a bit more about what is actually going on under the hood - so I am in some need of some guidance.
To this end, I have done some cursory reading on Tomcat and servlets from the following links:
What is a servlet
Difference between embedded and not embedded
Tomcat docs
So what I think I understand from this is that the servlets sit inside the Tomcat instance (a servlet container) and Tomcat handles all of the receiving all of the requests of the client and relaying them to the servlets. The servlets process the requests, send back a response, which Tomcat then sends back to the client. I suppose in the local setup I have, my machine would both be acting as the client and server.
Given the above I want to know:
How I can directly see and monitor the client's sending the request to Tomcat and verify that Tomcat has received the request? Essentially, how can I verify that this networking side of things is happening due to some implementation by Tomcat?
How does Tomcat parse the request information and send it to the servlets?
Is Tomcat a servlet container or a web server? Are these the same thing?
In the answer given in the second link regarding embedded vs. non-embedded, the answer states that an embedded server looks like a regular java program. Does this mean that for an embedded server, the server is in the java application while the web application is inside the server in the non-embedded case? Like the containment relationship is reversed? What does containment mean in the first place here?
Apologies for the numerous questions and thank you for helping to clarify.
2. How does Tomcat parse the request information and send it to the servlets?
The Servlet specification explains that in detail. The spec is surprisingly easy to read; I suggest giving it a go.
As a simplified overview…
The job of a Servlet container is to process the incoming request, which is just a bunch of text. The Servlet container pulls out the various pieces and assembles them into a request object.
Likewise, the response produced by your servlet is packaged up as a response object. The Servlet container's job is to use all the info contained in that object to create a stream of text to be sent back to the client web browser.
The whole point of Servlet containers is to relieve the servlet-writing programmers of the need to know much of the details of HTTP and how to make a server. The Servlet container does all that work. In other words, the great thing about Servlet technology is that you the programmer need not ask this # 2 question of yours!
3. Is Tomcat a servlet container or a web server? Are these the same thing?
(a) both, (b) no.
No, servlet containers and web servers are two different kinds of software.
A web server handles:
listening for incoming connections from clients (web browsers, etc.)
sending response back to the client
A web server handles all the network traffic.
A Servlet container provides an environment in which relatively small chunks of code (servlets) can process a request and formulate a response. The small servlet does not have to handle network traffic, launching & shutting down, security, and all the other responsibilities of a full server. That explains the "-let" in "Servlet".
Your servlet you write plugs into a Servlet container. The container communicates with the web server, receiving each request passed by the web server, and passing to the web server the response produced by your servlet. When a request arrives, the container invokes your servlet.
Your servlet remains blissfully ignorant as to what particular Servlet container implementation is running, as long as it complies with the Jakarta Servlet specification. And your servlet remains blissfully ignorant as to the existence of web servers.
Some products, such as Tomcat & Jetty, can be composed of both a web server and a Servlet container.
Tomcat is composed mainly of three components: (1) Catalina, a servlet container, (2) Coyote, a web server, and (3) Jasper, a Jakarta Server Pages processor. See Wikipedia.
For most people's needs, the Coyote web server in Tomcat is a suitable web server. So you can use Tomcat as as all-in-one application server, handling both web traffic and servlets.
[web request] ➜ [Tomcat Coyote] ➜ [Tomcat Catalina] ➜ [your servlet]
Alternatively, some folks choose to use Tomcat only as a Servlet container, sitting behind a separate web server such as Apache HTTP Server. In such a case, Tomcat’s Coyote component goes unused. Instead, the separate web server handles client browser components, and processes incoming requests. If a request is asking for a static resource, the web server serves it out, without any involvement from Tomcat. If the request is asking for work that has been assigned to a servlet, then the separate web server passes the request on to Tomcat and its Catalina component. After your servlet produces a response, the response moves from Tomcat back to the external web server, which traffics the response onwards to the client web browser.
[web request] ➜ [Apache HTTP Server] ➜ [Tomcat Catalina] ➜ [your servlet]
4 … embedded vs. non-embedded …
Non-embedded is the classic situation, as originally envisioned when Servlet technology was first invented.
Back then, servers were few, expensive, and already in place permanently. The goal of Servlet technology was to make it easy for companies to keep those expensive servers busy by having many web applications running alongside each other.
Servlet technology allowed many different servlets to be running on one machine without stepping on each other, and without the programmers of each servlet having known anything about the other servlets being written. The Servlet container can stay up and running as servlets are deployed and un-deployed.
Fast forward, and we have cloud technology where servers are many, cheap, and convenient to create and destroy on-the-fly. So nowadays many people want to run their web applications separately, one web app per virtual machine or virtual service. Thus the need for embedded mode. We need an application that can be launched and shutdown on its own, to run one specific servlet (or multiple servlets meant to work together) without any other unrelated web apps.
One way to achieve this new goal is to package a web server and servlet container into a standalone Java app. A system administrator can launch and quit this standalone app like any other Java app, without knowing anything about how to configure an on-going web server and Servlet container.

General session handling in java

I work with multiple languages like PHP, C, JAVA etc.
I am trying to fully understand how session is implemented in java.
Question : In php I can create a session and use the session in any class even utility classes. In Java why session is meant to be handled normally only in servlet classes? why not in any basic POJO classes without actually passing session or context object ? (I know other classes can, using complex techniques, but why not easily as servlet or as in other languages ).
I do know that the correct way to do things is use session in Servlet to retrieve object/values and pass object/values to other utility classes and get results.
Please let me know if there is in fact an easy way to get session in basic POJO classes.
I had flagged to delete this question as I found the answer in another SO link Retrieving Web Session from a POJO Outside the Web Container . Since it has not been deleted till now , I am answering my own question.
What are servlet and why we use them ?
A Java servlet is a Java program that extends the capabilities of a server.
Although servlets can respond to any types of requests, they most commonly implement applications hosted on Web servers.
Such Web servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET.
More information about java servlet at the link https://en.wikipedia.org/wiki/Java_servlet
Servlet vs POJO/normal Java classes ( why session access only in servlet ?)
A servlet class too is a java class. Servlet is a java class which comply servlet specification, so that it can be run in a server. Java class not required to comply Java Servlet API
A servlet container (e.g. Tomcat) is designed to be able to recognise servlet classes and, if configured correctly, treat them in a particular way. There's nothing magical about this - you can always write your own application that can treat any type of class in a specific way - but there's a standard that all servlet containers follow that means you can write a servlet class and know how it will be used.
Java was designed to be a platform independent language to create software to be embedded in various consumer
electronic devices. Soon it was apparent that the Java can be used in client/server programming environment because of its inherent portability. Hence servlet and applet were introduced in java. Java applets helped create dynamic, self-executing program on client side. Java servlet is a small program that executes on the server. Just as applets dynamically extend the functionality of a web browser, servlets dynamically extend the functionality of a web server.
Since servlet part of java is used in server side programming , this has access to session. POJO classes are also used in server side programming but code which uses session is usually written in servlet.
POJO Vs PHP classes(server side)
Java is used in many development environments like desktop,mobile,embedded and Web. PHP is a server-side scripting language designed for web development.It can also be used for general purpose programming but I am limiting the discussion to class used in php in server side programming(ie .php pages). So it is better to compare php classes in php pages with servlet/JSP - server side development part of JAVA.
Again servlet puts html code inside Java code. JSP puts java code inside html code. So JSP in java is more comparable to PHP .
If we understand this then we can infer that there is no good reason to compare POJO in java to PHP classes (server side). It is like comparing applies to oranges. if you are interested in comparison between php and jsp please visit http://www.withoutbook.com/DifferenceBetweenSubjects.php?subId1=57&subId2=2&d=Difference%20between%20PHP%20and%20JSP
In modern web development we use template engines ie instead of JSP we use servlet with template engine(Freemarker,Velocity). PHP has its own template engines.
How to access session in POJO classes ? Why it is hard / Is there any easy way ?
First of all it should not be done as it would be a bad programming practice. Secondly it can be done but there is hardly any scenario where this would be necessary. Since there is no need for the same the ways to implement it are not easy.
It should not be done , If you have to there are two ways.
We can pass session and request objects from servlet to POJO classes(via methods or constructors).
Store the request in a ThreadLocal in a Filter (and clean it afterwards)
Also look at the SO links for sample code
get HttpSession|Request from simple java class not servlet class
Retrieving Web Session from a POJO Outside the Web Container
I asked this question when I was stuck at a particular scenario . I used to program in PHP and wondered why I never had the issue there and got confused. People who usually move from php to java , also have a hard time understanding the how it all works , so attaching one more part to the answer.
Why servlets require servlet containers like tomcat while php runs on a http server/webserver (Apache).?
The question is incorrect in assumptions made but the answer will highlight why .
A web server is software that helps to deliver web content (web pages) to the clients (e.g. web browser)
through the Internet using HTTP protocol. HTTP is a simple request /response protocol underpinning most web
applications on the Internet, regardless of whether they are written in Java. An HTTP request can correspond to
one of the seven HTTP methods: GET, POST, HEAD, OPTIONS, TRACE, PUT and DELETE
The server side technologies like JSP/Servlets, ASP, and PHP etc. will require their software libraries to be installed
at the server. Without these libraries a web server won’t be able to execute those server technologies and is just an HTTP Server.
An HTTP server can handle HTML and other client side technologies which don’t require any server capability.
The Apache HTTP Server is an example of an HTTP server.
Regarding Servet/JSP : Servlet can be run on a servlet container.Tomcat is normally called a servlet container.A Servlet container is basically a web server for Java Servlets and JSP pages. Tomcat has an HTTP listener inside it, but in addition to that it has a servlet/JSP engine. A servlet, in the end, is a Java class. JSP files (which are similar to PHP, and older ASP files) are generated into Java code (Servlet), which is then compiled to .class files by the server and executed by the Java virtual machine.
Regarding PHP : PHP will not work in pure Apache or any HTTP server. We are able to run php in apache because it is linked to Apache(SAPI - server API) Using mod_php5.so(or similar) modules. We do not normally start any php daemon. Apache starts the php interpreter along with itself.
If oracle wants, it can develop a module similar to PHP module which will make it easier to host Java code on an HTTP server. But using a servlet container model has many benefits compared to the PHP interpreter model even with the disadvantage of increase in complexity.
From Servlet class pass session to any helper class(Create HTTPSession as helper class propertiy).
Now in your POJO class get session from helper class that you have set previously from servlet.
Question : In php I can create a session and use the session in any
class even utility classes. In Java why session is meant to be handled
normally only in servlet classes? why not in any basic POJO classes
without actually passing session or context object ? (I know other
classes can, using complex techniques, but why not easily as servlet
or as in other languages )
Every language has its own design & structure. There is no universal session handling protocol that it should be handled the same way in all the server side languages. In java if handling session in controllers i'e servlet setting & getting attributes serves your purpose why would anyone want to get a session object in a POJO. You can save a POJO in a session in a servlet.
Besides if there are exceptional out of the norm requirements you can always write custom code.

Which Web technologies allow for the development of static user interface/presentation layers

I have a Spring-WS web service that runs on a Jboss application server. I also have a Spring-MVC application running on a separate server running Jboss 7. The Spring-MVC application is used mainly for the user interface. The Spring-WS application contains all the services for the business logic.
The standard approach to use the components we have would be in the following order:
Client Browser ---(HTTP Request)---> Spring MVC ----(SOAP Request)-----> Spring-WS
Client Browser <---(HTTP Request)--- Spring MVC <----(SOAP Response)<----- Spring-WS
There is a requirement to change some of the requests coming from the Client Browser to go direct to the Web Services instead of via the Spring MVC application. The Spring MVC application will be used to load the initial presentation screens but the actions that involve any updates/writes will be going through the Spring-WS process.
To achieve this, we have a bespoke process which runs on the same machine as the client browser that traps all HTTP requests. Its purpose is to convert the request to a SOAP message and the response to a HTTP response. The path is shown below:
Initial Request (Retrieve presentation/user interface)
Client Browser ---(HTTP Request)---> Spring MVC
Subsequent requests
Client Browser ---(Http Request) ----> SOAP Converter (Local process) ------> (SOAP Request) ----> Spring-WS
Client Browser <---(Http Request) ---- SOAP Converter (Local process) <------ (SOAP Request) <---- Spring-WS
There are two paths that happen in the above scenario. The initial request to display the pages on the screen will be a request to the Spring-MVC process. Any subsequent requests that involve changing the data will be via the path shown above.
The problem that i have now is that all responses from Spring-WS (the webservice) are in XML format. This means that when the request comes from the browser, the data will have to come from the web service but the pages will need to be refreshed from the Spring-MVC application. This somehow feels a bit wrong as each request will involve to calls. One to get the data and one to get the presentation data.
To overcome this, i would like to implement the Spring-MVC layer using a technique where i only need to make one initial request. This means that the user interface will rendered on the screen. All subsequent requests to the Spring-WS service should not result in the browser to be rendered other than refreshing the data.
I am interested in knowing what kind of technologies i can use to achieve this. One way of doing this is by using Applets but this has been ruled out for security reasons. I have seen several websites which work exactly the way i have described above. i.e. the page never refreshes. A very good example is the Sonatype Nexus Maven repository manager user interface shown below:
It runs in a web browser and when the Nexus user interface loads on the browser, it is almost like a Swing type application. (Any one know what technology Nexus use for the user interface?)
I guess my question is which Web based user interface technologies (preferably open source) can i use which has a swing type look and feel but is not Swing and requires minimal requests to the server to refresh the screens?
Thanks in advance.
Nexus' interface is built using Sencha GXT3, which now also contains ExtJS.
Have a look at the GXT API: it contains a lot of web components which knows how to update their own state without doing the full request/response cycle (using Ajax), which is what you seem to be trying to get away from.

Java EE Web Applications vs Web Services

Can someone confirm or clarify for me:
From what I can tell, Java EE web apps consist of a Servlet and/or JSP driven dynamic web page being fed back in the HTTP response, triggered by the Java EE server receiving a HTTP GET or POST request.
From what I can tell, Java EE web services also make use of Servlets as the web tier components, however a WS Servlet receives a SOAP message and validates the contents of those messages with whatever WSDL the Servlet is WARed with. The response is also packaged in SOAP and sent back to the requestor.
So, from what I can tell, both Java EE web apps and WSes use Servlets as the web components, with the only real difference being the protocol used (raw HTTP vs SOAP, which is an extension of HTTP).
This is the best I could come up with - am I right? Totally wrong? Close?
The biggest difference is not in how they are delivered, but in how they are used. Web applications are targeted to web browsers and usually serve full HTML-based applications . Web services, on the other hand, are purposed to serve raw data for another application to consume.
You are right, and servlets are just a means to connect the code at a given webapp context to a URI path. Whether the servlet is serving HTML or WS is inconsequential to the webapp container, it's just data with a MIME type being sent back.
You could also manage the same thing with a filter under Servlet 2.3 and later specifications. Spring does this, and has a very robust means of adding to the filter chain to support it.

How can I run a servlet programme?

How can I run a servlet programme?How can i set the classpath?
Please read this http://www.jsptube.com/servlet-tutorials/simple-servlet-example.html for your first steps.
Well, you generally run it in a servlet container such as Websphere Application Server or Tomcat. And the way you configure the classpath depends on the servlet container you choose.
just to repeat the basics: a servlet is a piece of java code that runs inside a servlet container (i.e. a spezialized web server). it listens to client requests (typically issued through a web browser) and answers them with a response (e.g. with an HTML page).
"running" a servlet can thus mean two different things:
deploying the servlet to a servlet container (and thereby making it listen to requests), or
actually making it process a client request
(1) is achieved by packaging the servlet code (+ 3rd party libraries) into a war file and deploying this web application archive to the server -- details may vary depending on the type of servlet container. (2) is triggered by issuing a http request, e.g. through typing the servlet URL (something like http://localhost:8080/servletexample) into the location bar of your browser.

Categories

Resources