- JDK
- Tomcat
- Eclipse Java EE
- Goto Workbench
- New server wizard
- Select Tomcat 9
- Select JDK
- Give location of
- Start Tomcat (Stop the previously running tomcat as a prerequisite)
- File
- New
- Dynamic Web Project
- Give name as Hello World
- Context root: HelloWorld
- Finish
- Under Java Resources
- Right click > Servlet
- If there are errors, download and add
javax.servlet.jarto Classpath. - Run
- Choose the Server as Tomcat
- WebContent:
- Has Web content like html, jsp
- Adding a JSP
-
Right click on WebContent
-
New > JSP
-
Finish
-
Content:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www/w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%= new java.util.Date() %> </body> </html> -
Scriptlets:
-
Example 1:
<% java.util.Date today = new java.util.Date(); String text = "Today's date is: " + today; %> <%= text %> -
Example 2:
<% String text = "Today's date is: "; out.println(text); %> <%= new java.util.Date() %>
-
-
web.xml-
Servlet class:
demo.Test<servlet> <servlet-name>Test</servlet-name> <servlet-class>demo.Test</servlet-class> </servlet> <servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping> -
JSP file:
login.jsp<servlet> <servlet-name>Login</servlet-name> <jsp-file>/login.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
-
- Right click on project
- Export
- Select WAR file
- Wizard:
- Browse for location
- workspace/servlets2/Deployment
- Click Finish
- Copy the war file and paste it in Tomcat
webappsfolder - Stop Tomcat instance in Eclipse
- Restart tomcat
-
http://localhost:8080/Scriptlets/index.jsp?name=bla&id=123456 -
URLParams.java
public class URLParams extends HttpServlet { public void doGet(HttpRequest request, HttpResponse response) { String name = request.getParam("name"); String id = request.getParam("id"); PrintWriter out = response.getWriter(); out.println("Name is: " + name); out.println("id id: " + id); } } -
parameters.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www/w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name = request.getParam("name"); String id = request.getParam("id"); out.println("Name is: " + name); out.println("id is: " + id); %> </body> </html>
- New Jsp
-
manipulations.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www/w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Insert title here</title> </head> <body> <% for(int i = 0; i < 5; i++) { %> <p>This is run in a scriptlet loop: <%= i %></p> <% } %> <% String id = request.getParameter("id"); %> </body> </html>
-
-
include directive
-
<%@ include file="<file-path>" %>: like inline copy paste -
<jsp:include page="<file-path>" />: included only at runtime (variables will not be avialable at compile time)<% String id = request.getParameter("id"); %>
-
-
Including headers, footers (reusable)
-
New JSP includes.jsp
<body> <%@ include file="copyright.txt" %> <!-- including during translation of JSP into servlet --> </body> -
New file > copyright.txt
Copyright caveofprogramming.comincludeif file doesn't change very often- Run
-
Dynamic include: New file > updates.txt
Today's weather is: sunny (changes frequently)-
includes.jsp
<body> <p> <!-- Dynamic include: content changes a lot --> <jsp:include page="updates.txt" /> <!-- when jsp is served, browser sends another request for this --> </p> </body>
-
-
-
New JSP: variables.jsp
<% String name = "Sue"; %>-
To include this use
<%@ include ...>for variables (Java code) to be accessible<%@ include file="variables.jsp" %> <!-- like copy and paste --> <%= name %>
-