Basic Concepts:
Java Servlet: Is a Java class that serves HTML to a browser so the idea is that you start a servlet and somewhere in the world the user go the url in your wesbsite and that causes the http get request to your java servlet(java class). That class, runs continuously in your application server and then that will send some httml to the browser which the browser will display to the user
JSP: This page will actually be compile into a servlet by your application server engine so Tomcat is the one that is going to compile this page into a servlet before running it
War file: This is like a jar file which basically archives your entirely application in a format that can be deploy to the server
How to create a new project with servlet:
- Open New Project
- File->New->Dynamic Web Project
-
- Choose Project Name->Next->Next->Finish
- Create Servlet:
-
- Highlight the Java Resources folder
- Click on New
- Name the package (ex. gui)
- Name class name ->NExt
- Choose the methods to use (so far in tutorial left as default)
- How to print something from Servlet:
-
- on the method doGet type:
PrintWriter out = response.getWriter();
out.println(“Hello Gaby from Deployment2”);
- How to publish on browser:
-
- highlight the servlet class (java class)
- Click on run server
- it should open a browser with the input that you type on doGet method
- How to publish from an index.html or index.jsp file:
-
- Under webcontent, create a new file and call it (index.html or index.jsp)
- Type and input
- Where is web.xml located:
-
- WebContent -> Web-Ind -> web.xml
- Why is web.xml important:
-
- It has the servlet mapping stuff
- this servlet mapping controls what url your servlet is going to run on
- So if i change the default name in the url-pattern tag to have a custom name such as /gaby-example then when i run my servlet or jsp/html file it will point to that url
- This mappings tag must be there win the web.xml because if not then when we deploy to an app service on the internet, it will not work unless we have this xml stuff correct
- So that point of this xml is to deploy correctly
- What if I add a new jsp file and I want to add it to the web.xml mapping tags:
-
- You need to copy and paste the tags :
-
- Original snip to copy:
-
- <servlet>
<servlet-name>example</servlet-name>
<servlet-class>gui.example</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/gaby-example</url-pattern>
</servlet-mapping>
- <servlet>
- Modified pasted snip:
-
- <servlet>
<servlet-name>example</servlet-name>
<jsp-file>/login.jsp</jsp-file> # this is the asp file that I want to map
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/gaby-example</url-pattern>
</servlet-mapping>
- <servlet>