0% found this document useful (0 votes)
2 views

java slip 2

The first document is a Java program that collects and sorts a list of friends' names entered by the user, displaying them in ascending order. The second document is a servlet that handles HTTP GET and POST requests, providing client and server information, including the client's IP address, browser type, and the server's operating system, along with a list of loaded servlets. Both documents demonstrate fundamental Java programming concepts and web application development.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java slip 2

The first document is a Java program that collects and sorts a list of friends' names entered by the user, displaying them in ascending order. The second document is a servlet that handles HTTP GET and POST requests, providing client and server information, including the client's IP address, browser type, and the server's operating system, along with a list of loaded servlets. Both documents demonstrate fundamental Java programming concepts and web application development.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1

import java.util.*;

public class FriendNamesSorter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of friends: ");


int n = scanner.nextInt();
scanner.nextLine(); // Consume newline

HashSet<String> namesSet = new HashSet<>();

System.out.println("Enter the names of your friends:");


for (int i = 0; i < n; i++) {
String name = scanner.nextLine();
namesSet.add(name);
}

// Using TreeSet to sort the names in ascending order


TreeSet<String> sortedNames = new TreeSet<>(namesSet);

System.out.println("\nFriends' names in ascending order:");


for (String name : sortedNames) {
System.out.println(name);
}

scanner.close();
}
}

Q2

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestInfoServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Get client information


String clientIP = request.getRemoteAddr();
String browserInfo = request.getHeader("User-Agent");

// Get server information


ServletContext context = getServletContext();
String serverOS = System.getProperty("os.name");
// Get loaded servlets
Enumeration<String> servletNames = context.getServletNames();

out.println("<html><head><title>Request and Server


Info</title></head><body>");
out.println("<h2>Client Information</h2>");
out.println("<p><strong>IP Address:</strong> " + clientIP + "</p>");
out.println("<p><strong>Browser Type:</strong> " + browserInfo + "</p>");

out.println("<h2>Server Information</h2>");
out.println("<p><strong>Operating System:</strong> " + serverOS + "</p>");

out.println("<h2>Loaded Servlets</h2>");
out.println("<ul>");
while (servletNames.hasMoreElements()) {
out.println("<li>" + servletNames.nextElement() + "</li>");
}
out.println("</ul>");

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

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
doGet(request, response);
}
}

You might also like