0% found this document useful (0 votes)
9 views10 pages

WT 3,4,5 Saqs

The document covers key concepts in PHP, servlets, and JSP, including control structures, file handling, session management, and differences between cookies and sessions. It also discusses servlet lifecycle, session tracking techniques, and JSP elements such as directives and implicit objects. Additionally, it highlights the advantages of JSP over servlets and outlines the roles of Controller, View, and Model in web applications.

Uploaded by

artdemios73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

WT 3,4,5 Saqs

The document covers key concepts in PHP, servlets, and JSP, including control structures, file handling, session management, and differences between cookies and sessions. It also discusses servlet lifecycle, session tracking techniques, and JSP elements such as directives and implicit objects. Additionally, it highlights the advantages of JSP over servlets and outlines the roles of Controller, View, and Model in web applications.

Uploaded by

artdemios73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT – 3

1. What are the control structures in PHP?

Control structures in PHP include:

 Conditional statements: if, else, elseif, switch

 Loops: for, foreach, while, do-while

 Jump statements: break, continue, return

2. Difference between include() and require()?

 include(): Includes and evaluates a file; generates a warning if the file is not found but script
execution continues.

 require(): Includes and evaluates a file; generates a fatal error if the file is not found,
stopping script execution.

3. What is a function? How to pass the parameters to a function?

 A function is a block of code that performs a specific task.

 Parameters are passed to a function by placing them inside parentheses after the function
name, e.g.,

function greet($name) {

echo "Hello, $name!";

4. What are the super global variables?

Super global variables are predefined variables in PHP accessible from anywhere in the script.
Examples include:

 $_GET, $_POST, $_REQUEST

 $_SESSION, $_COOKIE

 $_FILES, $_SERVER, $_ENV

5. What is a cookie and its advantages?

 A cookie is a small piece of data stored on the client’s browser by the server.
Advantages:

1. Stores user-specific data for personalization.

2. Persistent data storage between sessions.


3. No server storage needed.

6. Difference between cookie and session?

 Cookie: Stored on the client-side; persists even after the browser is closed (if not expired).

 Session: Stored on the server-side; ends when the browser is closed unless explicitly
extended.

7. What are the MySQL functions? How to fetch data from the result set?

 MySQL functions in PHP: mysqli_connect(), mysqli_query(), mysqli_fetch_assoc(),


mysqli_fetch_row(), mysqli_close().

 To fetch data:

$result = mysqli_query($conn, "SELECT * FROM users");

while ($row = mysqli_fetch_assoc($result)) {

echo $row['name'];

8. How to read data from web form controls?

Data from web forms can be accessed using $_GET or $_POST arrays, e.g.,

$name = $_POST['name']; // for a POST method form

$email = $_GET['email']; // for a GET method form

9. Write about the output statements in PHP?

 echo: Outputs one or more strings.

 print: Outputs a string, returning 1 as success.

 print_r: Prints arrays and objects.

 var_dump: Outputs detailed information about variables.

10. What is a file? What are the file handling functions?

 A file is a collection of data stored on a disk.

 File handling functions include:

o fopen(), fread(), fwrite(), fclose()

o file_get_contents(), file_put_contents(), fseek()


11. Use of header() function?

The header() function is used to send raw HTTP headers to the browser. Common uses:

 Redirecting to another page: header("Location: page.php");

 Setting content types: header("Content-Type: application/json");

12. List the $_SERVER elements?

Examples of $_SERVER elements:

 $_SERVER['PHP_SELF']: Path of the executing script.

 $_SERVER['SERVER_NAME']: Name of the server.

 $_SERVER['HTTP_USER_AGENT']: Browser information.

 $_SERVER['REMOTE_ADDR']: IP address of the client.

 $_SERVER['REQUEST_METHOD']: HTTP request method used.

UNIT – 4

1. How does server-side programming differ from client-side programming?

 Server-side programming: Executed on the server; generates dynamic content and


communicates with databases. Example: Servlets, PHP.

 Client-side programming: Executed in the user’s browser; focuses on UI and interactions.


Example: JavaScript, HTML.

2. What is a servlet?

A servlet is a Java-based server-side technology used to create dynamic web content. It runs within a
servlet container like Apache Tomcat.

3. What are the advantages of servlets over CGI?

1. Performance: Servlets are multi-threaded; CGI creates a new process for each request.

2. Portability: Servlets are written in Java, making them platform-independent.

3. Scalability: Servlets are better for handling high user loads.

4. Ease of Maintenance: Servlets integrate well with Java’s robust APIs.


4. What is the task of the javax.servlet.Servlet interface?

The javax.servlet.Servlet interface defines methods for initializing a servlet, processing requests, and
destroying it. Key methods include:

 init(), service(), destroy().

5. What are the session tracking techniques?

1. Cookies

2. URL Rewriting

3. Hidden Form Fields

4. HTTP Session API

6. Describe how HTTP servlet handles its client request?

1. Receives the request via the service() method.

2. Delegates to doGet() or doPost() depending on the HTTP method.

3. Processes the request and prepares a response.

4. Sends the response back to the client.

7. What is the difference between servlet and applet?

 Servlet: Server-side, no GUI, runs on a server.

 Applet: Client-side, GUI-based, runs in a browser or applet viewer.

8. What are the advantages of cookies over URL Rewriting?

1. Ease of Use: Cookies are automatically managed by the browser.

2. Security: URL rewriting exposes data in the URL.

3. Efficiency: No need to modify URLs in all links.

9. Explain the life cycle of a servlet?

1. Initialization: init() is called to initialize resources.

2. Request Handling: service() handles requests and invokes doGet() or doPost().

3. Destruction: destroy() is called to clean up resources before the servlet is removed.


10. Can we use Java as the CGI language? If yes, how?

Yes, Java can be used for CGI scripting by creating standalone Java programs to handle HTTP requests
and responses. However, servlets are preferred for performance and scalability.

11. Define cookie and session?

 Cookie: A small piece of data stored on the client’s browser by the server.

 Session: A server-side mechanism to store user-specific data during a single interaction.

12. Difference between <init-param> and <context-param>?

 <init-param>: Defines parameters specific to a servlet.

 <context-param>: Defines application-wide parameters accessible by all servlets.

13. Difference between HttpServletRequest and HttpServletResponse?

 HttpServletRequest: Represents the client's request, allowing access to data like parameters
and headers.

 HttpServletResponse: Represents the server's response, enabling response customization


like setting headers and content.

14. Difference between HttpServlet and GenericServlet?

 HttpServlet: Specific to HTTP protocol; provides doGet() and doPost() methods.

 GenericServlet: Protocol-independent and requires overriding the service() method.

15. What are the four types of JDBC Drivers?

1. Type 1: JDBC-ODBC Bridge Driver

2. Type 2: Native API Driver

3. Type 3: Network Protocol Driver

4. Type 4: Thin Driver (Pure Java)

16. What is the use of web.xml or deployment descriptor?

Defines servlet mappings, initialization parameters, filters, and other web application configurations.

17. How can you use PreparedStatement and Statement?


 Statement: Executes static SQL queries.

 PreparedStatement: Executes parameterized queries, preventing SQL injection. Example:

PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE id = ?");

ps.setInt(1, userId);

ResultSet rs = ps.executeQuery();

18. Describe the procedure to retrieve the data from ResultSet?

1. Execute a query to get a ResultSet.

2. Use methods like next(), getString(), getInt() to retrieve data. Example:

while (rs.next()) {

System.out.println(rs.getString("name"));

19. Write a short note on Type 4 Driver.

 A Type 4 JDBC Driver is a pure Java driver that directly communicates with the database
using its protocol.

 Advantages: High performance, platform independence.

UNIT – 5

1. Key difference between HttpServletResponse.sendRedirect() and <jsp:forward>?

 HttpServletResponse.sendRedirect(): Sends a redirect response to the client, causing the


browser to make a new request to a different URL.

 <jsp:forward>: Forwards the request from one resource to another within the same server
without involving the client.

2. Short notes on JSP processing:

 A JSP page is translated into a servlet by the JSP engine.

 This servlet is compiled into a class file and loaded into memory.

 During runtime, the servlet processes client requests and generates dynamic content.

3. What are the Action elements or standard tags?


Action elements in JSP are predefined tags used to control the behavior of the JSP page. Examples
include:

 <jsp:include>: Includes another resource.

 <jsp:forward>: Forwards a request.

 <jsp:useBean>: Instantiates or locates a bean.

 <jsp:setProperty> and <jsp:getProperty>: Manipulates bean properties.

4. What is a bean?

A JavaBean is a reusable software component that follows specific conventions, including:

 A public no-argument constructor.

 Private fields with public getter and setter methods.

5. Difference between cookie and session?

 Cookie: Stored on the client-side; data persists even after the browser is closed.

 Session: Stored on the server-side; ends when the browser is closed unless explicitly
extended.

6. What are the session tracking techniques?

1. Cookies

2. URL Rewriting

3. Hidden Form Fields

4. Session API

7. Short notes on implicit JSP elements for Expression Language (EL):

 EL simplifies access to application data.

 Implicit objects include:

o pageContext: Provides access to page attributes.

o param: Maps request parameters.

o session, application: Provide access to session and application attributes.

8. Explain error handling and debugging in JSP page:

 Error Handling:
o Use isErrorPage and errorPage attributes in JSP directives.

o Configure error pages in web.xml.

 Debugging:

o Use logging frameworks like Log4j.

o Use JSP implicit objects like exception to capture errors.

9. List the JSP elements:

1. Directives: <%@ directive ... %>

2. Declarations: <%! ... %>

3. Scriptlets: <% ... %>

4. Expressions: <%= ... %>

5. Action Tags: <jsp:action ...>

6. Custom Tags: Defined by tag libraries.

10. Example for implicit objects (request, response, out, exception):

jsp

Copy code

<%= request.getParameter("name") %> <!-- Access a request parameter -->

<%= response.encodeURL("home.jsp") %> <!-- Encode a URL -->

<%= out.println("Hello, World!") %> <!-- Print to output -->

<%= exception.getMessage() %> <!-- Display an exception message -->

11. What are the scripting elements in JSP?

1. Declarations: <%! ... %>

2. Scriptlets: <% ... %>

3. Expressions: <%= ... %>

12. What are the Directive elements in JSP?

1. Page Directive: <%@ page ... %> (e.g., language, import).

2. Include Directive: <%@ include ... %> (includes a file during translation).

3. Taglib Directive: <%@ taglib ... %> (declares a tag library).


13. How to declare and use expression statements in JSP?

 Declarations: Use <%! ... %> to declare variables or methods.

jsp

Copy code

<%! int count = 0; %>

 Expressions: Use <%= ... %> to output values.

jsp

Copy code

<%= count %>

14. Advantages of JSP over Servlets:

1. Easier to write and maintain due to separation of HTML and Java.

2. Allows use of custom tags and tag libraries.

3. Automatically translated into servlets, providing the benefits of Java.

15. Applications of JSP:

1. Dynamic web content generation.

2. E-commerce applications.

3. Data-driven websites with database integration.

16. Role of Controller, View, and Model:

1. Controller: Manages user input and interacts with the model.

2. Model: Represents the application's data and business logic.

3. View: Displays the data to the user.

17. Explain the life cycle of JSP:

1. Translation: JSP is converted into a servlet.

2. Compilation: Servlet is compiled into a Java class.

3. Initialization: jspInit() is called.

4. Execution: service() handles requests.


5. Destruction: jspDestroy() cleans up resources.

4o

You might also like