Advanced Java Programming Exam Solutions
Advanced Java Programming Exam Solutions
PreparedStatement is used for executing precompiled SQL queries, allowing parameterized queries to avoid SQL injection attacks. CallableStatement is used to call stored procedures in a database, supporting OUT parameters. For instance, PreparedStatement can execute 'SELECT * FROM users WHERE id=?', whereas CallableStatement can execute a stored procedure 'CALL getUser(?)' .
Spring MVC architecture is based on the Model-View-Controller pattern. It handles web requests through a DispatcherServlet, which coordinates with controllers to process requests, delegates views to render results, and utilizes models for data manipulation. This framework offers flexibility and modularity by decoupling components, simplifying the development and testing of web applications .
The InetAddress class in Java represents an IP address. It facilitates the handling of IP addresses by providing methods like 'getByName()' to obtain an InetAddress object corresponding to a host name, and 'getLocalHost()' to retrieve the loopback address of the local machine .
The Servlet life cycle consists of initialization (init()), request handling (service()), and removal (destroy()). The web container manages these methods by loading the servlet class, invoking init() for initialization, calling service() to handle requests, and invoking destroy() before unloading. It ensures servlets operate efficiently by managing threading and resource cleanup .
Filters are used in Servlets for preprocessing requests and postprocessing responses, often applied for tasks such as logging, authentication, input validation, or data compression. They intercept requests before they reach the servlet and responses before they leave the servlet, enhancing functionality without modifying code directly .
Dependency injection is a design pattern in which an object's dependencies are provided externally, promoting loose coupling and easier testing. In Java, frameworks like Spring automatically inject instances into classes using annotations like '@Autowired', reducing boilerplate code and increasing flexibility .
JSP implicit objects are predefined variables provided by the JSP container to handle different functionalities. Significant ones include 'request' for HTTP request data, 'response' for HTTP response, 'session' for user session tracking, and 'application' for servlets context interaction. They simplify coding by providing direct access to key objects without additional setup .
The 'include' directive includes a specified page at compile time, meaning the content is inserted into the servlet during translation, while 'jsp:include' action tag includes content at request processing time or runtime, allowing dynamic updates. An example of 'include' is '<%@ include file="header.jsp" %>', while 'jsp:include' is '<jsp:include page="footer.jsp" />' .
HQL (Hibernate Query Language) is an object-oriented query language using class-based queries and supports seamless integration with Hibernate ORM, whereas SQL is a relational database query language focusing on tables and fields. HQL is advantageous in Hibernate for database portability, while SQL is commonly used for direct database manipulation .
HTTP GET requests data from a specified resource and appends parameters to the URL, visible and limited in size, making it less secure. POST sends data in the request body, supporting larger amounts and not visible in the URL, making it more suitable for secure data transmission .