|
| 1 | +Author: Ross Studtman |
| 2 | + |
| 3 | +Tutorial: John Purcell's "Servlets and JSPs: Creating Web Applications With Java" |
| 4 | +John's website: CaveOfProgramming.com |
| 5 | +Notes for videos 3 through 7 |
| 6 | + |
| 7 | +Video #3: Creating a servlet |
| 8 | +Video #4: Creating a JSP page |
| 9 | +Video #5: Understanding the web.xml file |
| 10 | +Video #6: Deploying servlet to local Tomcat server. |
| 11 | +Video #7: Deploying to internet. |
| 12 | + |
| 13 | + |
| 14 | +VIDEO #3 Notes: |
| 15 | + |
| 16 | + Create a servlet. |
| 17 | + |
| 18 | + I created the following to demonstrate this: |
| 19 | + |
| 20 | + * gui -- a package |
| 21 | + * Video_3_servlet.java -- inside gui package |
| 22 | + |
| 23 | + [ ] Video_3_servlet.java |
| 24 | + |
| 25 | + Notice the doGet() and doPost() methods receive two arguments: |
| 26 | + |
| 27 | + HttpServletRequest request, HttpServletResponse response |
| 28 | + |
| 29 | + We can use these objects for a variety of things. Such as creating a PrintWriter object. |
| 30 | + |
| 31 | + |
| 32 | + doGet(): |
| 33 | + |
| 34 | + Create a PrintWriter object: |
| 35 | + |
| 36 | + PrintWriter out = response.getWriter(); |
| 37 | + |
| 38 | + |
| 39 | + Create some output so when this servlet is run we see a result: |
| 40 | + |
| 41 | + out.println("<html>"); |
| 42 | + out.println("<h1>Greetings from the doGET method!!</h1>"); |
| 43 | + out.println("</html>"); |
| 44 | + |
| 45 | + |
| 46 | + Run the servlet: |
| 47 | + |
| 48 | + Select the servlet's tab in Eclipse, click the green run button in Eclipse's toolbar. |
| 49 | + |
| 50 | + First time running it you'll get a dialog pop up: |
| 51 | + |
| 52 | + Choose existing server radio button; |
| 53 | + Select Tomcat v7.0 Server at localhost (it's State says "Started"); |
| 54 | + Always use this server when running this project check box; |
| 55 | + |
| 56 | + Another dialog may pop up: "Server: The server may need to be restarted. Do you want to..." |
| 57 | + |
| 58 | + Restart server radio button; |
| 59 | + Remember my decision check box; |
| 60 | + |
| 61 | + Notice the address bar in the browser: |
| 62 | + |
| 63 | + http://localhost:8080/JSP_Tutorial_1/Video_3_servlet |
| 64 | + |
| 65 | + http://localhost:8080 --> domain |
| 66 | + JSP_Tutorial_1 --> context root |
| 67 | + Video_3_servlet --> servlet |
| 68 | + |
| 69 | + If you open another browser and paste in that address you'll get the same result. |
| 70 | + That is, this *is* the URL address for reaching this servlet. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +VIDEO #4 Notes: |
| 75 | + |
| 76 | + Creating a .jsp file. |
| 77 | + |
| 78 | + John notes that it is not "normal" to spit out HTML directly from the java program as we did |
| 79 | + in Video #3. Normally the HTML is found inside a JSP file. JSPs are where your HTML goes. |
| 80 | + |
| 81 | + In the JSP_Tutorial_1 project folder is the WebContent folder and this is where .jsp files |
| 82 | + should go. |
| 83 | + |
| 84 | + I created the following for this tutorial: |
| 85 | + |
| 86 | + * Video_4_htmlFile.html |
| 87 | + * Video_4_jspFile.jsp |
| 88 | + |
| 89 | + [ ] Video_4_htmlFile.html |
| 90 | + |
| 91 | + This is a regular, plain Jane HTML file. |
| 92 | + |
| 93 | + R-click WebContent -> New -> HTML: name it. |
| 94 | + |
| 95 | + Run your HTML file: |
| 96 | + |
| 97 | + Select the tab in Eclipse, click green run button. |
| 98 | + |
| 99 | + Admire your HTML in the browser. |
| 100 | + |
| 101 | + [ ] Video_4_jspFile.jsp |
| 102 | + |
| 103 | + R-click WebContent -> New -> JSP File: name it. |
| 104 | + |
| 105 | + A JSP page is an HTML page into which you can embed java code. JSPs are compiled into servlets |
| 106 | + by the application server (Tomcat). |
| 107 | + |
| 108 | + This is an expression tag: |
| 109 | + |
| 110 | + <%= stuff goes here %> |
| 111 | + |
| 112 | + The expression tag will write directly to the HTML page. Put strings in here. |
| 113 | + |
| 114 | + For example, this: |
| 115 | + |
| 116 | + <%= new java.util.Date() %> |
| 117 | + |
| 118 | + will invoke the Date object's toString() and put the date string directly in the HTML. |
| 119 | + |
| 120 | + |
| 121 | + Note: Eclipse may automatically import Util.Date at the top of the jsp: |
| 122 | + |
| 123 | + <%@page import="java.util.Date"%> |
| 124 | + |
| 125 | + This is a scriptlet tag: |
| 126 | + |
| 127 | + <% java code here %> |
| 128 | + |
| 129 | + A scriptlet lets you run java code inside it. |
| 130 | + |
| 131 | + An example of using a scriptlet: |
| 132 | + |
| 133 | + The scriptlet: |
| 134 | + |
| 135 | + <% |
| 136 | + Date today = new Date(); |
| 137 | + String todayText = "Today is: " + today.toString(); |
| 138 | + |
| 139 | + out.println("<ul><li>'out' is a JspWriter object.</li></ul>"); |
| 140 | + %> |
| 141 | + |
| 142 | + Use variable that is inside the scriptlet: |
| 143 | + |
| 144 | + <p>This paragraph is outside the scriptlet but we can use stuff from inside the scriptlet:</p> |
| 145 | + <ul> |
| 146 | + <li><%= todayText %></li> |
| 147 | + </ul> |
| 148 | + |
| 149 | + |
| 150 | + Note: In Eclipse use ctrl+space to show pre-defined variables of a scriptlet. |
| 151 | + |
| 152 | + |
| 153 | + Run your .jsp file |
| 154 | + |
| 155 | + |
| 156 | +VIDEO #5 Notes: |
| 157 | + |
| 158 | + Understanding the web.xml file. |
| 159 | + |
| 160 | + When creating a new Dynamic Web Project there is a choice to "Generate Web.xml deployment descriptor". |
| 161 | + John suggests creating this because the annotation in the servlet, @WebServlet("/Video_3_servlet"), |
| 162 | + may not be recognized by some server applications. So he prefers to use the Web.xml file. Additionally, |
| 163 | + later on we'll have a need to manipulate the web.xml file. |
| 164 | + |
| 165 | + *** Instead of making a new project I chose to google how to add a Web.xml if my project doesn't contain one: |
| 166 | + |
| 167 | + source: |
| 168 | + |
| 169 | + http://crunchify.com/eclipse-missing-web-xml-file-how-can-i-create-web-xml-in-eclipse/ |
| 170 | + |
| 171 | + Procedure: |
| 172 | + |
| 173 | + R-click project --> Java EE Tools --> Generate Deployment Descriptor Stub |
| 174 | + |
| 175 | + This creates "web.xml" file in WEB-INF |
| 176 | + |
| 177 | + I created the following for this video: |
| 178 | + |
| 179 | + * Video_5_servlet.java |
| 180 | + * index.jsp |
| 181 | + * myLoginPage.jsp |
| 182 | + |
| 183 | + [ ] Video_5_servlet.java |
| 184 | + |
| 185 | + After creating this file look at the line immediately above the class declaration and you'll |
| 186 | + see the following: |
| 187 | + |
| 188 | + @WebServlet("/Video_5_servlet") |
| 189 | + |
| 190 | + Comment that out: |
| 191 | + |
| 192 | + //@WebServlet("/Video_5_servlet") |
| 193 | + |
| 194 | + We don't want that active. Commenting that out will give a warning that there's an import that isn't |
| 195 | + used, ctrl-shft-o to organize imports. |
| 196 | + |
| 197 | + |
| 198 | + If we run this servlet now we'll get a 404 HTTP status error: The requested resource is not available. |
| 199 | + This is because we now need to add a reference to this servlet into the web.xml file. |
| 200 | + |
| 201 | + |
| 202 | + [ ] web.xml |
| 203 | + |
| 204 | + Located in WebContent/WEB-INF/web.xml |
| 205 | + |
| 206 | + Map a URL address to my servlet: |
| 207 | + |
| 208 | + In web.xml create <servlet> & <servlet-mapping> tags for the Video_5_servlet |
| 209 | + located in the "gui" package: |
| 210 | + |
| 211 | + <servlet> |
| 212 | + <description></description> |
| 213 | + <display-name>NameSeenInAnAdminTool_Maybe</display-name> |
| 214 | + <servlet-name>kitten</servlet-name> |
| 215 | + <servlet-class>gui.Video_5_servlet</servlet-class> |
| 216 | + </servlet> |
| 217 | + |
| 218 | + <servlet-mapping> |
| 219 | + <servlet-name>kitten</servlet-name> |
| 220 | + <url-pattern>/one</url-pattern> |
| 221 | + </servlet-mapping> |
| 222 | + |
| 223 | + |
| 224 | + Names given to <url-pattern> can basically be whatever you want, but whatever name you give |
| 225 | + here is the URL address that points to this servlet. |
| 226 | + |
| 227 | + The <servlet-name> can also be whatever you want, as long as it is unique in the web.xml file. |
| 228 | + All it does is link the <servlet> and <servlet-mapping> tags, so the program knows what goes |
| 229 | + with what. |
| 230 | + |
| 231 | + |
| 232 | + [ ] Video_5_servlet.java |
| 233 | + |
| 234 | + Now run this servlet. |
| 235 | + |
| 236 | + Notice the address bar in the browser: |
| 237 | + |
| 238 | + http://localhost:8080/JSP_Tutorial_1/one |
| 239 | + |
| 240 | + Now if you type this into the address bar: |
| 241 | + |
| 242 | + http://localhost:8080/JSP_Tutorial_1/Video_5_servlet |
| 243 | + |
| 244 | + ...it won't work, the 404 "The requested resource is not available." is reported. |
| 245 | + So now, as far as the server is concerned, the URL address for the Video_5_servlet is |
| 246 | + http://localhost:8080/JSP_Tutorial_1/one |
| 247 | + |
| 248 | + |
| 249 | + [ ] myLoginPage.jsp |
| 250 | + |
| 251 | + Very simple .jsp. |
| 252 | + |
| 253 | + Make a web.xml entry for this page: |
| 254 | + |
| 255 | + <servlet> |
| 256 | + <description></description> |
| 257 | + <display-name></display-name> |
| 258 | + <servlet-name>MyLoginPage</servlet-name> |
| 259 | + <jsp-file>/myLoginPage.jsp</jsp-file> |
| 260 | + </servlet> |
| 261 | + <servlet-mapping> |
| 262 | + <servlet-name>MyLoginPage</servlet-name> |
| 263 | + <url-pattern>/two</url-pattern> |
| 264 | + </servlet-mapping> |
| 265 | + |
| 266 | + |
| 267 | + Two things to note: |
| 268 | + |
| 269 | + 1) in the <servlet> tag it isn't a <servlet-class> but rather a <jsp-file>, and |
| 270 | + it requires the ".jsp" extension. |
| 271 | + |
| 272 | + 2) URL address for this page is: http://localhost:8080/JSP_Tutorial_1/two |
| 273 | + |
| 274 | + |
| 275 | + [ ] index.jsp |
| 276 | + |
| 277 | + R-click WebContent --> New --> JSP File --> name it. |
| 278 | + |
| 279 | + This was created to demonstrate that if you have one of these files then a address that |
| 280 | + only has the context root will go here: |
| 281 | + |
| 282 | + This: |
| 283 | + |
| 284 | + http://localhost:8080/JSP_Tutorial_1/ |
| 285 | + |
| 286 | + Goes to the index.jsp. |
| 287 | + |
| 288 | + Why? Because in the web.xml there is a welcome file list: |
| 289 | + |
| 290 | + <welcome-file-list> |
| 291 | + <welcome-file>index.html</welcome-file> |
| 292 | + <welcome-file>index.htm</welcome-file> |
| 293 | + <welcome-file>index.jsp</welcome-file> |
| 294 | + <welcome-file>default.html</welcome-file> |
| 295 | + <welcome-file>default.htm</welcome-file> |
| 296 | + <welcome-file>default.jsp</welcome-file> |
| 297 | + </welcome-file-list> |
| 298 | + |
| 299 | + ...and index.jsp is on it. (this list was automatically created and represents popular names |
| 300 | + for HTML pages that "direct" users to other pages within the web site. |
| 301 | + |
| 302 | + |
| 303 | + Run the context root: |
| 304 | + |
| 305 | + R-click the project folder, JSP_Tutorial_1, --> Run As --> Run On Server |
| 306 | + |
| 307 | + Doing that will make a call to the Tomcat server using only the context root for the URL. |
| 308 | + |
| 309 | + |
| 310 | + NOTE: There's a small summary section on this topic in JSP_Tutorial_8's notes, right at the top. |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | +VIDEO #6 Notes: |
| 315 | + |
| 316 | + Deploying the servlet to my local Tomcat server. |
| 317 | + |
| 318 | + ...which isn't much different than what Eclipse was doing, says John, but it is good for debugging. |
| 319 | + |
| 320 | + |
| 321 | + A WAR file is like a .jar file for deploying onto a server application (like Tomcat). |
| 322 | + |
| 323 | + Export program as a WAR file: |
| 324 | + |
| 325 | + - R-click project --> export --> WAR file --> save somewhere. |
| 326 | + - Paste WAR file into Tomcat 7.0/webapps |
| 327 | + - Stop Eclipse version of Tomcat. |
| 328 | + - Start Tomcat service: use "Monitor Tomcat" program to do that. |
| 329 | + - Open browser and type into address bar: |
| 330 | + |
| 331 | + Address: Result |
| 332 | + |
| 333 | + server: http://localhost:8080 TomCat web page |
| 334 | + context root: http://localhost:8080/JSP_Tutorial_1 contents of index.jsp |
| 335 | + Video_5_servlet.java: http://localhost:8080/JSP_Tutorial_1/one what was written in the doGet() method |
| 336 | + myLoginPage.jsp: http://localhost:8080/JSP_Tutorial_1/two contents of MyLoginPage.jsp |
| 337 | + |
| 338 | + Context root is probably the name of your project (though you could have changed that when |
| 339 | + you first constructed the project using the wizard). |
| 340 | + |
| 341 | + NOTE: when you paste your WAR file into TomCat's webapps folder and then run Tomcat, Tomcat then makes |
| 342 | + a folder named the same thing as your WAR file. So if you want to delete the WAR file then also delete |
| 343 | + the folder generated by Tomcat that has the same name as the WAR file. |
| 344 | + |
| 345 | + |
| 346 | + VIDEO #7 Notes: |
| 347 | + |
| 348 | + I used Cloudbees, like the video. I researched using other sites for hosting java applications but I ran into |
| 349 | + a world I know very little about and the jargon threw me for a bit of a loop. |
| 350 | + |
| 351 | + Cloudbees notes: |
| 352 | + |
| 353 | + After having set up an account, |
| 354 | + |
| 355 | + Applications --> click "Add Application" tab at the top --> name it, Cloudbees builds a shell or something |
| 356 | + for an application --> look for the label mid-page that says "Upload Application" --> click "Choose File" |
| 357 | + --> select your WAR file --> and that's about it. |
| 358 | + |
| 359 | + Click your hyperlink to your website found near hte top of the page --> your website opens in a new |
| 360 | + browser --> try appending some of the url-names given to your files in web.xml into the browser. |
| 361 | + |
| 362 | + *** Admire that you have uploaded to the world wide internety thingymajig an application/website that you |
| 363 | + have created. And it works! |
| 364 | + |
| 365 | + - Now celebrate. |
| 366 | + |
| 367 | + THE END of JSP_Tutorial_1. |
0 commit comments