Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 26 Answers


A) Write a java program to display list of college names from college table. (Assume

College table (CID, CName, addr) is already created.

Answer :

import java.sql.*;

 

class Slip26A {

    public static void main(String args[]) throws Exception {

 

        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bcadb", "root", "");

 

        Statement stmt = con.createStatement();

 

        ResultSet rs = stmt.executeQuery("select cname from college");

        System.out.println("<<<<College Name>>>>");

        System.out.println("===================");

        while (rs.next()) {

            System.out.println(rs.getString(1));

        }

        con.close();

    }

}

 

 

B) Write a SERVLET program to Design an HTML page containing 4 option buttons

(Painting, Drawing, Singing and swimming) and 2 buttons reset and submit. When the

user clicks submit, the server responds by adding cookie containing the selected hobby

and sends the HTML page to the client. Program should not allow duplicate cookies to

be written.

Answer :

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hobbyServlet")
public class HobbyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String COOKIE_NAME = "selectedHobby";

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>My Hobbies</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Select your favorite hobby:</h3>");
        out.println("<form method='post'>");
        out.println("<input type='radio' name='hobby' value='painting' /> Painting<br>");
        out.println("<input type='radio' name='hobby' value='drawing' /> Drawing<br>");
        out.println("<input type='radio' name='hobby' value='singing' /> Singing<br>");
        out.println("<input type='radio' name='hobby' value='swimming' /> Swimming<br>");
        out.println("<input type='submit' value='Submit' />");
        out.println("<input type='reset' value='Reset' />");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String selectedHobby = request.getParameter("hobby");

        // Check if the cookie with the same name already exists
        boolean cookieExists = false;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(COOKIE_NAME)) {
                    cookieExists = true;
                    break;
                }
            }
        }

        // If cookie does not exist, create it and add it to the response
        if (!cookieExists) {
            Cookie hobbyCookie = new Cookie(COOKIE_NAME, selectedHobby);
            hobbyCookie.setMaxAge(60 * 60 * 24 * 365); // set cookie to last 1 year
            response.addCookie(hobbyCookie);
        }

        // Display a message to the user
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>My Hobbies</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Your selected hobby is: " + selectedHobby + "</h3>");
        out.println("<p>Cookie created successfully!</p>");
        out.println("</body>");
        out.println("</html>");
    }
}

Output :


Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 26 Answers Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 26 Answers Reviewed by technical_saurabh on December 15, 2022 Rating: 5

No comments:

Powered by Blogger.