1 - kuroski.net

advertisement

Practice Test Questions

1. A Java Servlet is running at ____________ site.

2. Method type ____________________ is the default HTTP request type?

3. URL of _______________ type request can be bookmarked.

4. _________________ request objects are passed to a Servlets service() method?

5. The _______________object of an HTTP request can be shared by all requests in the same session.

6. The _______________object of an HTTP session can be shared by all sessions in the same Web

application.

7. Servlet life cycle methods are ________, ________, ____________

8. ___________ and ___________ are the two most often used method for an HTTP Servlet to retrieve

the request and respond to the request

9.

Sketch out a doPost() method signature.

10. The signature of setAttribute() method is ______________________________

11. The signature of getAttribute() method is ______________________________

12. How to use HttpServletRequest object to get value in a HTTP “input” parameters?

13. Output a “Hello World HTML” with Servlet code

14. How a Servlet dispatches a request to another Web component with shared data?

15. Describe some ways how a Web client can access a Java Servlet.

16. Sketch out a Servlet code fragment to store a user name in a cookie.

17. Any HTML page can be converted to a JSP page by change its file extension to _______________

18. ____________________ JSP tag can have Java statements.

19. The attribute object of _______________ can be shared by all requests in the same session.

20. ________________ JSP action tag can also forward a control to a static HTML page

21. A JSP file is automatically re-compiled at the time _________________________________

22. ________________ translates JSP into its Servlet Java file.

23. How to initialize a JSP life cycle and terminate it?

24. How to access a JSP in a HTML form?

25. Sketch out a simple JSP page which prompt for user name, says Hello <username>.

26. Describe how to use comments in JSP.

27. What is JSP declaration element?

28. What is JSP expression scripting element ?

29. What is JSP page directive ?

30. What is JSP scriptlets element

?

Answer Key

1. Server

2. GET

3. GET

4., ServletRequest

5. Request

6. Application

7. init(), service(), destroy()

8. doGet(), doPost()

9. void doPost( HttpServletRequest request, HttpServletResponse response )

throws ServletException, IOException;

10. public void setAttribute (java.lang.String key,java.lang.Object o)

//Stores an attribute in the context of this request. Attributes are reset when the request is switched.

11. public Object getAttribute (String name)

//Returns the value of the named attribute as an Object

.

12.

protected void doPost( HttpServletRequest request,

HttpServletResponse response )

throws ServletException, IOException

{

String feet = request.getParameter(“input” );

13. PrintWriter out = response.getWriter();

out.println( "<html><head><title>Processing get requests with " +

"data</title></head><body><h1>Hello World</h1></body></html>" );

out.close(); // close stream to complete the page

}

14. MyObject myObject = new MyObject(…) request.setAttribute("myData", myObject);

RequestDispatcher rd;

rd = getServletContext().getRequestDispatcher(targetURL);

rd.forward(request, response);

. . .

15. a.

Use an HTTP form. You can specify the HTTP Servlet in the action attribute of the <form> tag. The default request type is the GET method. The POST method can also be specified in the method attribute.

b. For GET type HTTP requests you can also directly type

http://<host>/<servletName

> in the

URL location field of the browser if you don’t want to pass in any parameter values.

c. If you do need to pass in some parameter values with the request you can type

http://<host>/<servletName>?<paramterName>=<value

> such as

http://localhost:8080/conv/conversion?feet=100

. There may be more then one pair of <parameter name>/<value> which are separated by “&”. The “?” indicates the beginning of the query string.

d. Another way is to have a hyperlink tag

<A HREF=”http://<host>/<servletName>Click me to get Servlet</A> in a HTML file so you can access the Servlet by clicking on the link. You can attach a query string to the

<servletName> in the hyper link tag.

16.

public class CookieServlet1 extends HttpServlet {

void doPost( HttpServletRequest request,

HttpServletResponse response )

throws ServletException, IOException

{

String uname = request.getParameter( "username" );

Cookie cookie = new Cookie( “name”, uname );

Cookie.setMaxAge(5*60); //The cookie will expire in 5 minutes

response.addCookie( cookie );

out.println( " . . . " );

. . .

}

}

17. jsp

18. scriptlets

19. session

20. <jsp:forward>

21. When the JSP is changed or the JSP is accessed at first time

22. JSP engine

23.

<%! public void jspInit(){ . . . } %>

<%! public void jspDestroy(){ . . . } %>

24.

<form action="/jsp/helloJsp.jsp">

...

</form>

25.

<html>

<%-- This is a Simple JSP (comment from JSP tag -- %>

<body>

Hello <%= request.getParameter("userName") %>!

<br>

</body>

</html>

26.

<%-- . . . %> is a JSP comment scripting element which

stays on the server.

27. JSP declaration element <%! . . . %> which tells the JSP engine to insert the enclosed java code into the

generated Servlet java source code somewhere outside of any method.

28. The expression scripting element <%= . . . %> will display values of the enclosed expression in string

format

29. The JSP page directive directs the JSP engine to import the page during the Servlet translation .

30. JSP scriptlets are embedded within <% ...

The syntax is <% Java code %>

%> tags. They are executed at the runtime.

Download