Download 1 Do not delete anything from this file. Just write your answer under

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Do not delete anything from this file. Just write your
answer under the word: Answer.
Make a class named Main.java after through the following steps. From step: 10 is related
to your Main.java. Copy/paste your Main.java under the word: Answer.
1. Make an empty folder for eclipse.
2. Make a server (Tomcat v7.0 Server).
3. Make a dynamic project named: Project
4. Make an html document in eclipse named: Main.html.
5. Replace the content of Main.html by the following:
<<!-- Main.html. Do not change this html document -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>To display name and age</title>
</head>
<body>
<form action="http://localhost:8080/Project/Main" method = "get">
Enter Your Name: <input type="text" name="yourName"><br>
Enter Your Age&nbsp;&nbsp;&nbsp;&nbsp;:<input type="text"
name="yourAge"><br>
<input type="submit" value="Click to submit">
</form>
</body>
</html>
6. Make a servlet named: Main.java.
7. Replace the class Main.java by the following.
Note: Only replace the class Main.java. Everything else is intact.
import
import
import
import
import
import
import
javax.servlet.ServletException;
javax.servlet.annotation.WebServlet;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
java.io.*;
java.util.Enumeration;
@WebServlet("/Main")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
public Main() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
2
Enumeration<String> names = request.getParameterNames();
String name = (String)names.nextElement();
String value = request.getParameter(name);
String name2 = (String)names.nextElement();
String value2 = request.getParameter(name2);
System.out.println(value + " " + value2);
}
}
8. Run the Main.html and enter John Doe for name and 45 for age to get:
9. Click on
to go back and enter Jane Doe for name and 22 for age to get:
3
10. Change Main.java to displays an html document such that when you run Main.html
and enter John Doe for the name and 45 for the age the display should be:
11. When you click on:
and enter Jane Doe and 22 the display should be:
4
12. When you click on:
and enter Jack Smith and 54 the display should be:
Note:
 If we continue entering names and ages we should get a list of the names and ages
on the html page.
5

The above list of names and ages are sample examples. Your Main.java should
work for any number of names and ages.
Hints:
 Your Main.java should make an html string such that if you copy/paste in an html
document the above pages should show up.
 You need a private instance variable: s of type String, initialized by:"". Plan to
display the content of: s. Therefore the content of: s on the first display should be
a name and age.
 Every time you click on
and go back, entering a name and age they should be
concerted (By operation: +) to: s
 You also need to add to: s the html tag: <br>. It stands for: Line beak. This tag
moves the writing marker to the next line.
 You need to print on the page other lines to make an html page (See page 19-20
of the file: “Installing Apache.doc”.
Answer.