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



A) Write a java program using multithreading for the following:

1. Display all the odd numbers between 1 to n.

2. Display all the prime numbers between 1 to n.

Answer :

import java.io.*;

 

public class Slip29A {

    public static void main(String args[]) {

        try {

            DataInputStream br = new DataInputStream(System.in);

            System.out.println("********* java program using multithreading *********");

            System.out.print("Enter Number : ");

            int n = Integer.parseInt(br.readLine());

            OddNumber a1 = new OddNumber(n);

            a1.start();

            PrimeNumber a2 = new PrimeNumber(n);

            a2.start();

        } catch (Exception e) {}

    }

 

}

 

class OddNumber extends Thread {

    int odd;

 

    OddNumber(int n) {

        odd = n;

    }

 

    public void run() {

        System.out.println("****** Odd Numbers *******");

        for (int i = 1; i <= odd; i++) {

            if (i % 2 != 0) {

                System.out.print(i + " | ");

            }

        }

        System.out.println();

    }

}

 

class PrimeNumber extends Thread {

    int prime;

 

    PrimeNumber(int n){

        prime = n;

    }

    public void run() {

        System.out.println("****** Prime Numbers *******");

        for (int i = 2; i <= prime; i++) {

            boolean isPrime = true;

            for (int j = 2; j <= i / 2; j++) {

                if (i % j == 0) {

                    isPrime = false;

                    break;

                }

            }

            if (isPrime == true)

                System.out.print(i + " | ");

        }

    }

}


B) Write a SERVLET program to change inactive time interval of session.

Answer :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ChangeSessionTimeoutServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // Set content type
    response.setContentType("text/html");

    // Get current session
    HttpSession session = request.getSession();

    // Get current timeout value
    int currentTimeout = session.getMaxInactiveInterval();

    // Print current timeout value
    PrintWriter out = response.getWriter();
    out.println("<html><body>");
    out.println("<h3>Current Session Timeout: " + currentTimeout + " seconds</h3>");

    // Display form to change timeout value
    out.println("<form method='post' action='" + request.getContextPath() + "/ChangeSessionTimeoutServlet'>");
    out.println("Enter new timeout value (in seconds): <input type='text' name='timeout'>");
    out.println("<input type='submit' value='Change'>");
    out.println("</form>");

    out.println("</body></html>");

  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // Get new timeout value from form data
    int newTimeout = Integer.parseInt(request.getParameter("timeout"));

    // Get current session
    HttpSession session = request.getSession();

    // Set new timeout value
    session.setMaxInactiveInterval(newTimeout);

    // Redirect to doGet method to display updated timeout value
    response.sendRedirect(request.getContextPath() + "/ChangeSessionTimeoutServlet");

  }

}

Output :


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

No comments:

Powered by Blogger.