Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Web Programimi Servlet and JSP demo.

Similar presentations


Presentation on theme: "Java Web Programimi Servlet and JSP demo."— Presentation transcript:

1 Java Web Programimi Servlet and JSP demo

2 Veglat JDK 1.7 MySQL 5.x MySQL Connector J Tomcat 6.x Eclipse

3 Building Web Applications Using Servlets and JSP
Start (add) web server from tomcat Or from eclipse

4

5 A HelloWord Servlet New Dinamic Web Project HelloWord project Name
Target runtime: Tomcat v7 Dynamic Web Module 2.5 Contex Name and root project WebContent On Java resources right click and add Servlet Package gui; class HelloWorld; doGet doPost Init and destroy extra info

6 In HelloWorld servlet class
PrintWriter out = response.getWriter() Ctrl+shift+o for imports out.println("<html>"); out.println("<b>Hello World</b>"); out.println("</html>"); Go to start… Open in browser Do view source

7 In HelloWorld Dinamic Web Project
Web content folder? New html page => HelloWorld.html Change the Title and Body content If Servlet are simply html in java Then JSP is java in html

8 HelloWorld JSP Add new JSP file to WebContent
JSP are compiled to servlet by Tomcat Change the title and body to show the Hello World text in bold Now some Java <p> <%= "Hello world JSP from Java".toUpperCase() %> </p> <%= new java.util.Date() %> <body> <% java.util.Date today = new java.util.Date(); String toShow = "Today’s date is: "+today.toString(); %> out.println("<p>"); out.println(toShow); out.println("</p>");

9 Deployment Descriptors the Web.xml File
File New Dynamic Project (keep defaults) Project Name Temp1 Check the Generate web.xml Add Servlet name: Test Start Url mapping is by default done directly in servlet Temp1 is context root (content folder) Test is resource name This is problematic since deployment to “real” servers will not work.

10 Deployment Descriptors the Web.xml File
File New Dynamic Project Name: Deployment Dynamic web module version: 2.5 Keep defaults Check the Generate web.xml Add Servlet name: Hello To doGet() method add this: PrintWriter out = response.getWriter(); out.println("Hello from Servlet"); Execute the servlet Url is The first part is servers address The second part is mapping which is defined in web.xml

11 Web.xml Is in WebContent->WEB-INF
To open right click and open with text editor <welcome-file-list> … defines the home-pages Add a jsp file index.jsp to WebContent folder Go to root folder in url ( Some words for servlet-name, servlet-class and servlet mapping to url-pattern Change the /Hello to /helloWorld Observe… The application server on real servers use this file in order to make real mappings

12 Web.xml Add login.jsp to WebContent
Add a text in login.jsp “this could be a login page” In web.xml copy and paste <servlet>…</servlet-mapping> Change to <servlet-name>Login</servlet-name> And to <url-pattern>/loginpage</url-pattern> Change class-name line to => <jsp-file>demo.Hello</jsp-file> As a result: <servlet> <description></description> <servlet-name>Login</servlet-name> <jsp-file>/login.jsp</jsp-file> </servlet> <servlet-mapping> <url-pattern>/loginpage</url-pattern> </servlet-mapping>

13 Deployment Right click to project select export
WAR file to desktop name it DeploymentDemo Copy file to …Desktop\APP\apache-tomcat \webapps which is the home of the tomcat http server Go to eclipse and stop local tomcat server!!! Go to Desktop\APP\apache-tomcat \bin and start the tomcat general server: startup.bat Go to And give a try to loginpage, helloWorld Observe the folder structure of the webapps folder

14 Importing Java Classes Into JSPs
Import standard java classes Import your specific classes page import="java.util.Date, gui.*" %> <%= new Date() %> <% TextOutput textOut = new TextOutput(); out.println(textOut.getInfo()); %> package gui; public class TextOutput { public String getInfo() { return "This is returned from a method of a class in the gui package."; }

15 Getting URL Parameters
Mechanism for passing data into your servlets So add new servlet named URLParameters The same applies to jsp Add new jsp file named parameters.jsp // String user = request.getParameter("user"); String id = request.getParameter("id"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("The 'user' parameter is: " + user); out.println("<br/>"); out.println("The 'id' parameter is: " + id); out.println("</html>"); ======================== <% String name = request.getParameter("name"); // out.println("Name param is: " + name); %>

16 Scripting HTML Html generators <% for(int i=0; i<5; i++) { %>
<p>This HTML is in a scriptlet loop, iteration: <%= i %></p> <p/> <% } %> <% String id = request.getParameter("id"); if(id == null) { %> <strong>ID parameter missing</strong> <% } else { %> ID parameter is: <%= id %>

17 Including Files In Other Files
Create file includes.jsp Suppose you have copyright.txt file to include Static include Create copyright.txt Copyright aab.com Dynamic include updates.txt: Today's weather is: sunny JSP includes Create new jsp file: Variables.jsp <% String name = "Sue"; %> Runtime file including idnotfound.html : The ID parameter is missing. Idfound.html : The ID parameter was found. <!-- static include: content changes infrequently --> include file="copyright.txt" %> <p/> <!-- dynamic include: content changes a lot --> <jsp:include page="updates.txt" /> <!-- Must use static include if it's got java code in it that we want to access --> include file="variables.jsp" %> <%= name %> <!-- Must use include jsp tag if you don't know what file you want till run time --> <% String id = request.getParameter("id"); %> <% if(id == null) { %> <jsp:include page="idnotfound.html" /> <% } else { %> <jsp:include page="idfound.html" /> <% } %>

18 Forwarding and Redirecting
To demonstrate forwarding add new jsp file forward.jsp Add the code from notes And observe the URL Hello <!-- Forward to another page (server side; the browser never sees forward.jsp, only index.jsp, but the url remains forward.jsp) the using jsp:forward tag --> <jsp:forward page="index.jsp"></jsp:forward> <% // forward to another page (server side) via Java // request.getRequestDispatcher("index.jsp").forward(request, response); // redirect to another page (client side) via Java. The browser will briefly // see forward.jsp, before going to the url for index.jsp // response.sendRedirect("index.jsp"); %>

19 Model 1 vs. Model 2 Architecture
Start by creating new dynamic web project and name it MVC Add following JSP files to project login.jsp, about.jsp, error.jsp Model 1 architecture simply uses one starting jsp file and then by using forwarding and redirecting makes things work This model is unscalable and unstable so the model 2 is proposed which is also known as a MVC model Controller is servlet which makes routing So add an servlet in package controller name it Controller Now add index.jsp login.jsp => This is the login page. about.jsp => This is the about page. error.jsp => The action parameter is missing. Index.jsp => <p>This is the home page.</p> <p><a href="/MVC/Controller?action=login">Go to login page.</a></p> <p><a href="/MVC/Controller?action=about">Go to about page.</a></p> Controller.java String action = request.getParameter("action"); String page = null; if(action == null) { page = "/error.jsp"; } else if(action.equals("about")) { page = "/about.jsp"; else if(action.equals("login")) { page = "/login.jsp"; else { getServletContext().getRequestDispatcher(page).forward(request, response);

20


Download ppt "Java Web Programimi Servlet and JSP demo."

Similar presentations


Ads by Google