Today, I came to know about servlets which is a technology to create web applications. Before starting with what things I came to know today, I would like to mention come concepts about server.
So, what’s basically a server?
Server is a computer software(not a hardware, I had this misconception) that runs on powerful machines which is responsible for providing services or you can say handling the request of client(computer program requesting for services from server). We have mainly two types of server- Application and Web server.
- Web server: Web server is an internet server for handling HTTP request and responding to them(HTTP response). This kind of server can send response as static HTML page. It doesn’t have capability to generate dynamic HTML pages. Examples of web server- Apache Tomcat, Apache Web server, nginx, IIS web server.
- Application server: Application server is superset of web server having plenty of other features such as Connection Pooling, Object Pooling, Transaction Support, Messaging services. When client requests for dynamic content(having database connectivity) then application server comes into picture. Examples of application server- JBoss, WildFly, Glassfish.
If one is interested for knowing the differences between them, go through the links.
http://www.javaworld.com/article/2077354/learn-java/app-server-web-server-what-s-the-difference.html
https://stackoverflow.com/a/936257/6499635
Servlet:
Servlet is typically a java program which manages the code on server. It’s the technology for creating web applications typically generating the dynamic web pages.
Install Apache tomcat in eclipse:
- Download server. I am using apache tomcat(web server). Download it from here.
- Extract the download zip folder. It can be renamed.
- Come to eclipse. Switch to Java EE perspective.
- Go to Windows -> Show view -> Servers.
- Then in the servers view, right click and choose New -> server.
- In dialog box, expand Apache and choose the Apache tomcat server from the list below. Make sure, the downloaded version of Tomcat is same as the one you are choosing from the list here. Click Next.
- Browse for the directory where tomcat folder is located which you extracted few steps back. Click Next. Finish.
- You have successfully installed tomcat in eclipse.
Create a project:
- Click File -> New -> Dynamic Web Project.
- Name the project. Click Next.
- Again click Next.
- Then tick the Generate web.xml deployment descriptor checkbox.
- Click Finish.
Some important points:
- Add the HTML, CSS, Javascript, images, JSP files in Web content directory.
- Add the Servlet class, other java classes in Java resources directory.
- Add the libraries in lib directory inside WEB-INF directory(this is private directory).
- If you checked the checkbox for generating web.xml file, you can find that in WEB-INF directory by expanding that.
Create a servlet class:
- Go to Java Resources in project explorer for your respective project.
- Right click src.
- Click New -> Servlet. Name the Servlet class. Click Next.
- You can add new URL mappings for the servlet class. Click Next.
- Choose the methods you want to include in the servlet.
- Click Finish.
Now, you will get the class which extends HttpServlet. On top of class name, you will see the annotation @WebServlet. This will contain the list of URL mappings. These mapping names are used in the HTML forms(in action attribute) or wherever you want to execute the code of this servlet class.