Professional Documents
Culture Documents
Advanced Sample Exam
Advanced Sample Exam
Advanced Sample Exam
private int n;
public void init() {
n = 0;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String name = request.getParameter(“uname”);
n++;
PrintWriter out = response.getWriter();
out.print(‘this page is accessed ’+ n +’times from the user’+name);
}
int []a;
this.start = start;
this.end = end;
this.a = a;
sum += a[i];
}}
a[i] = 1;
start = i * chunck;
threads[i].start();
int totalSum=0;
threads[i].join();
totalSum += threads[i].sum;
} System.out.println(totalSum); }}
GenericServlet HTTPServlet
class implements Servlet, ServletConfig and extends the GenericServlet class and
Serializable interfaces. implements Serializable interface
provides the implementation of all the part of the javax.servlet.http package
methods of these interfaces except the service It provides http specific methods such as
() method doGet, doPost
can handle any type of request so it is functionality of this class makes it easier to
protocol-independent. build servlets that work with HTTP requests
create a generic servlet by inheriting the and responses
GenericServlet class and providing the
implementation of the service method.
forward () method (of the RequestDispatcher the sendRedirect () method (of the
interface) HttpServletResponse interface)
Forwards a request from a servlet to another interface can be used to redirect response to
resource (servlet, JSP file, or HTML file) on another resource, it may be servlet, jsp or
the server. html file.
interface provides the facility of sending the works at client side because it uses the URL
request to another resource bar of the browser to make another request
a) b)
1. class MyThread implements Runnable 1. class ABC extends JDBC
2. { char ch; int n; 2. { public static void main (String[ ] args)
3. MyThread (char c, int n) 3. { Class.forName ("com.mysql.jdbc.Driver");
4. { ch = c ; n = n; } 4. Connection con=DriverManager.
5. public void start( ) getConnection ("jdbc:mysql:///test", "root",
6. { for (int i=0 ; i<n ; i++) "secret");
7. System.out.print(ch); 5. Statement stm = con.createStatement( );
8. } } 6. String s = “select * from Student”;
9. class Program 7. ResultSet rs = stm.updateQuery(“s”);
10. { public static void main (String[ ] a) 8. for (int i=0 ; i<rs.length ; i++)
11. { Thread t = new Thread (MyThread); 9. {System.out.print (“ID:” + rs.getInt(0));
12. T.start (‘z’, 50 ); 10. System.out.println (“name:” + rs.getInt(1));}
13. T.start (‘a’, 10); 11. s = “select * from Student where acYear>1”;
14. } 12. int n = stm.updateQuery(“s”);
15. } 13. System.out.println(“You inserted n new
Students”);
14. }
Name: “id”
Name: “name”
Name: “acYear”
Name: “gpa”
A Java Bean called Student containing the data members: {id, name, acYear, gpa}.
A JSP page called (process.jsp) that is called when clicking on the submit button at
index.html. This JSP page should do the following:
o If the user entered all data in the form:
Create an object of Student initialized by the data entered at the page index.html.
Insert the data of the created object into the table StudentInfo.
Return back to the index.html page.
o If the user left the ID input text empty:
Return back to the index.html page with an error message.
o If the user entered the value 99999 at the ID input text:
All the data of the students whose GPA> = 3.0 are displayed in a new JSP page called
result.jsp.
Best Wishes