COMPUTING SUBJECT: Servlet Basics TYPE: Assignment IDENTIFICATION: Servlet COPYRIGHT: Michael Claudius LEVEL: Easy TIME CONSUMPTION: 2 hours EXTENT: 50 lines OBJECTIVE: Servlet html i/o declaration PRECONDITIONS: Core Servlets & Servlet Ch. 1, 3, 4 COMMANDS: IDENTIFICATION: SERVLET/MC The Mission You are to make a Web project based on some .jsp page files and a few Java classes in a related package. Useful links for this assignment http://www.coreservlets.com Site for all kind of information http://volume1.coreservlets.com/archive/index.html Code examples from the book http://pdf.coreservlets.com/ The whole book in pdf format. Hints on Settings If you during the assignment get an error like ”Service denied. Port already in use”, then the port numbers used by the Tomcat/GlassFish server are reserved or already in use and must be changed. In Netbeans: Select Tools -> Servers And change the port numbers to e.g. server port = 8084 and shutdown port = 8024. 1. Create a Web project In your favorite editor create a new project named CoreServlets of the category Web and with the type Web Application. Look at Web Pages directory. Notice that the index.jsp file is automatically created in the Web Pages directory. This is where all the .jsp files will be placed. In the Source Package create a new package named “coreservlets” for the servlet .java classes. 2. Deploy the project to the Tomcat/Glassfish server To get the Tomcat/Glassfish server running Select CoreServlet project -> Click on right mouse -> Select Deploy Project In case of problems see hints on settings above Activate index.jsp; i.e. compile and try to run it. It will take some time as Tomcat now will be initialized. First step first… 3. Downloading You are now to use some predefined servlet .java files (Ch3 Java Files.zip and Ch4 Java Files.zip) which you can download from your teacher’s home page. Unzip the .java files to a folder. In Netbeans the servlets have to be registered in web.xml and when operating the servlets have to be registered in the web.html file. Therefore it is easiest to create new classes in Netbeans and then copy and paste the content of the doGet method in the downloadd class into the content of the processRequest method. This has been done for “HelloWorld” class but otherwise not. Note: In the next assignments you are to compile run various programs but the most important is that you try to understand the code; e.g. by answering the questions given. SO DONT RUSH ….. 4. HelloWorld Create a new Servlet class named “HelloWorld”. Copy and paste all of the downloaded version into this file. Compile and run “HelloWorld.java” To give default parameters for the request: and the parameters can be given. web.xml can also be used for parameter initialization: Select CoreServlets -> WEB-INF -> web.xml Double click on web.xml a dialog box pops up. Choose Servlets: How can parameters be initialized in the browser per default ? Fill out a default parameter for the execution. Look at the source file. What is request, response and out used for ? What is the purpose of the method setContentType ? 5. HelloServlet1 Create a new Web servlet file “HelloServlet1”.Copy and paste the content of the doGet –method in the downloaded version into “HelloServlet1”. Compile and run HelloServlet1.java. What is the purpose of the variable docType ? When is processRequest called ? 6. HelloServlet3 and ServletUtilities Create and compile HelloServlet3 and ServerUtilities.java. Run. How is the default set up of the server facilities done ? 7. LotteryNumbers Compile and run LotteryNumbers.java What is the purpose of the method init ? When is init called ? 8. UserIds Compile and run UserIds Why is concurrent access a problem in this code ? How can synchronization solve the problem ? Why is nextId not static ?
© Copyright 2026 Paperzz