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

ADVANCED JAVA LAB - E0122039

This document contains code for an advanced Java lab assignment involving numbers divisible by two parameters. The HTML includes inputs to collect two numbers and buttons to trigger GET and POST requests. The JSP file contains Java code to generate a list of numbers between 1-100 divisible by the first parameter but not the second. The servlet handles the POST request, collects the two numbers, and generates the output table directly instead of using a JSP file.

Uploaded by

mailadwaitharun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

ADVANCED JAVA LAB - E0122039

This document contains code for an advanced Java lab assignment involving numbers divisible by two parameters. The HTML includes inputs to collect two numbers and buttons to trigger GET and POST requests. The JSP file contains Java code to generate a list of numbers between 1-100 divisible by the first parameter but not the second. The servlet handles the POST request, collects the two numbers, and generates the output table directly instead of using a JSP file.

Uploaded by

mailadwaitharun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ADVANCED JAVA LAB – 1 WORK

HTML

<html>
<head>
</head>
<body>
<label for="a">Enter value for 'a': </label>
<input type="text" id="a">

<br>

<label for="b">Enter value for 'b': </label>


<input type="text" id="b">

<br>

<button onclick="handleButtonClick('GET')">Get Numbers</button>


<button onclick="handleButtonClick('POST')">Post Numbers</button>

<br>

<div id="result"></div>
</body>
</html>

.jsp file

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-


8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>

<%
int a = Integer.parseInt(request.getParameter("a"));
int b = Integer.parseInt(request.getParameter("b"));

List<Integer> result = new ArrayList<>();


for (int i = 1; i <= 100; i++) {
if (i % a == 0 && i % b != 0) {
result.add(i);
}
}
%>

<!DOCTYPE html>
<html lang="en">
<title>Result</title>
</head>
<body>
<table border="1">
<tr>
<th>Result</th>
</tr>
<% for (int num : result) { %>
<tr>
<td><%= num %></td>
</tr>
<% } %>
</table>
</body>
</html>

Servlet:

package sret.eo122004;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/PostNumbersServlet")
public class PostNumbersServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


IOException {
int a = Integer.parseInt(request.getParameter("a"));
int b = Integer.parseInt(request.getParameter("b"));

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<!DOCTYPE html>");
out.println("<html lang=\"en\">");
out.println("<head>");
out.println("<meta charset=\"UTF-8\">");
out.println("<meta name=\"viewport\" content=\"width=device-width, initial-
scale=1.0\">");
out.println("<title>Result</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border=\"1\">");
out.println("<tr><th>Result</th></tr>");

for (int i = a; i <= b; i++) {


out.println("<tr><td>" + i + "</td></tr>");
}

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

You might also like