Skip to content

2 Servlet

plor10 edited this page Nov 14, 2018 · 1 revision

WEEK 2: WEB APPLICATIONS

Matt Green MS.Ed, Instructor at WCTC

What is a Java web application

2

¨ Servlets, filters, listeners

¨ JSPs (another type of servlet)

¨ Static content (images, CSS, JavaScript)

¨ Metadata information

¨ Library dependencies

Servlets

3

¨ Receive web (i.e., HTTP) requests

¤ In the case of a web application, request contains data submitted from HTML forms

¨ Process the request (logic, data model, database, other services) ¨ Respond with an HTTP response ¤ In the case of a web application, the response is HTML ¨ NOTE: servlets are singletons ¤ Q: How does a web server handle multiple simultaneous requests? ¤ A: Threads ¤ Therefore: DO NOT store state (instance data) which should not be shared between requests

Java Server Pages (JSPs)

4

¨ Think of a JSPs as an executable HTML with code

¨ Embedded code can be Java (we’ll see this is pretty ugly)

¨ Embedded code can also be tags from standard tag libraries or custom tags

¨ JSPs are compiled at runtime by the web server

¤ First to Java

¤ Second to bytecode

¨ Basically, JSPs are another form of servlet

Metadata

5

¨ Web applications contain WEB-INF

¤ web.xml file which contains definitions of n Servlets n Filters n Listeners n Welcome pages n Resources n etc…

¤ WEB-INF also contains n Compiled classes n JAR files that your application requires

Creating a Java web application in IntelliJ 6

¨ File / New Project…

¨ We will do it this way this week (no guarantees next week)

Java EF

Structure of a Java web

r----

application

•FirstWebApp 1i liP Project

  • T FirstWebApp -/Projects/FirstWebApp Q. .:.:1 .. .idea !!I .. out .. src T .. web T .. WEB-INF ( web.xml index.jsp 1\ FirstWebApp.iml IIIII External Libraries

WAUKESHA coti:EGE

How web applications work

8

¨ Request/response

¨ HTTP Verbs

¤ GET

n Data can be sent on the same line as the URL

¤ POST

n Another way to request a response from the server

n Data is sent as part of the request message itself

¤ There are more which we will revisit with REST services but these will suffice for now

HTTP GET Example

9

https://www.google.com/search?source=hp&ei=Ii1uWvb0HMG10ASQz7eYB Q&q=html5+doctype&oq=&gs_l=psy- ab.3.0.35i39k1l6.12164.13002.0.17705.5.4.0.0.0.0.329.329.3- 1.2.0..2..0...1.1.64.psy- ab..3.2.1564.6..0j0i20i264k1j0i131i67k1j0i67k1j0i131k1.1235.yvXe bgTk2Qw

¨ Everything in red after the question mark is the content (aka parameters)

¨ Disadvantages

¤ Limited length to the URL

¤ Insecure n User can see this data in the address bar

HTTP POST Example

10

headers

POST / HTTP/1.1

Host: foo.com

Content-Type: application/x-www-form-

urlencoded Content-Length: 13

verb + requested resource + http version

say=Hi&to=Mom

content

¨ Advantages

¤ Larger amounts of data (unlimited) can be sent

¤ Secure

Java EF Create a Servlet --c=======-----------------------

New

Cut [j} Copy R Copy Path J Copy Reference J Paste

Find Usages F7 Find in Path... D F Replace in Path... D R Analyze

Refactor

Add to Favorites Show Image Thumbnails

Reformat Code Optimize Imports Delete...

Build Module 'FirstWebApp' Rebuild '' D F9

Local History ''Synchronize 'src'

Reveal in Finder

c Java Class ,. Kotlin File/Class §!I File

package-info.java

Ruby Class HTML File Stylesheet JavaScript File TypeScript File CFML/CFC file .;, CoffeeScript File JavaFXApplication Singleton a Erb File " XSLT Stylesheet Edit File Templates... GUI Form Dialog Form Snapshot ,11 Resource Bundle ,, XML Configuration File

• • New Servlet Name: FirstServlet prefix and suffix are taken from Java EE Names

Package: --------------- u

edu.wctc.dj.f irst.FirstServlet

Create Java EE 6 annotated class

l cancel ) -

v• .... Compare With... I Open Module Settings Mark Directory as

WAUKESHA COUNTY TECHNICAL COLLEGE

Diagram D [Q Google Guice

5 Data Source Servlet

Create a Servlet

12

¨ Open FirstServlet.java and add Java EE 6 JARs to module dependencies

Create a Servlet

13

¨ Open web.xml and add servlet mapping

FirstServlet /first

HTML Page

14

Create web/test.html

<title>Title</title>

Run and Test

15

¨ Assumption: Glassfish is installed! ¨ Create run configuration

click the +

Run and Test

16

¨ Select the Glassfish domain

Run and Test

17

¨ Add your web application to the deployments

click the +

Hit OK!

Run and Test

18

¨ Run

Here or here

Open test.html from Browser

19

¨ Enter URL http://localhost:8080/FirstWebApp_war_expl oded/test.html

¨ View Page Source if you want

¤ You should see your forms

¨ Enter data in first text box and click GET

¤ Should take you to an empty page but look at the address bar!

¨ Hit the back button and do the same for POST

¤ Should take you to an empty page and look at the address bar

Generate a Response from Servlet 20

¨ Implement doGet and doPost

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String html = "

Hi. I received param1=" + request.getParameter("param1") + " via POST";

response.getOutputStream().println(html); }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String html = "

Hi. I received param1=" + request.getParameter("param1") + " via GET";

response.getOutputStream().println(html); }

NOTE: When you makes changes you need to either redeploy your application or restart the Glassfish server completely.

Project

21

¨ Do the following for the project for this week

  1. Follow the remaining slides step by step to create a web application

  2. Copy your HTML pages from week 1 into the web folder n Redploy and make sure you can open your web pages from the browser

  3. Figure out how you can change the application context in the browser URL n Right now, it’s pretty ugly (e.g., FirstWebApp_war_exploded) n There is an IntelliJ setting in the Run configuration

  4. Commit and push all of your work to a ‘week2’ branch on github.com

¨ You should be able to accomplish this all in class ¤ Make sure to do it in class since I’m sure you will run into some issues and we can resolve them as they come up!

Java EF

WAUKESHA coti:EGE

Clone this wiki locally