ja2
- Write a java program to read āNā names of your friends, store it into HashSet and
display them in ascending order. [15 M]
import java.util.*;
public class FriendNames {
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 character
HashSet<String> friendSet = new HashSet<>();
for (int i = 0; i < n; i++) {
System.out.print("Enter name of friend " + (i + 1) + ": ");
String name = scanner.nextLine();
friendSet.add(name);
}
List<String> sortedFriendList = new ArrayList<>(friendSet);
Collections.sort(sortedFriendList);
System.out.println("Friend names in ascending order:");
for (String friend : sortedFriendList) {
System.out.println(friend);
}
scanner.close();
}
}
- Design a servlet that provides information about a HTTP request from a client, such as
IP-Address and browser type. The servlet also provides information about the server on
which the servlet is running, such as the operating system type, and the names of
currently loaded servlets.
import java.io.;
import javax.servlet.;
import javax.servlet.http.;
import java.util.;
public class HttpRequestInfoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>HTTP Request Information</title></head><body>");
out.println("<h1>HTTP Request Information</h1>");
out.println("<h2>Client Information:</h2>");
out.println("IP Address: " + request.getRemoteAddr() + "<br>");
out.println("Browser Type: " + request.getHeader("User-Agent") + "<br>");
out.println("<h2>Server Information:</h2>");
out.println("Server Name: " + request.getServerName() + "<br>");
out.println("Server Port: " + request.getServerPort() + "<br>");
out.println("Server Info: " + getServletContext().getServerInfo() + "<br>");
out.println("<h2>Currently Loaded Servlets:</h2>");
Enumeration<String> servletNames = getServletContext().getServletNames();
while (servletNames.hasMoreElements()) {
out.println(servletNames.nextElement() + "<br>");
}
out.println("</body></html>");
out.close();
}
}