Wednesday, 24 January 2018

Advancedjava-Slip30



Slip30

Q1. Write a JSP program to display all the prime number’s between 1 to n in “Blue” Color.      


prime.html

<html>
<head><title>JSP Page</title></head>
<body><br><br><center>
 <form action="prime.jsp ">
<h1>Enter the no :: <input type=text name=n ><br><br>
<input type=submit value="Submit"></h1>
</form></center>
    </body>
</html>

prime.jsp
<html>
<head><title>JSP Page</title></head>
<body><center><h1>The required Result is:: </h1>
<h2>
<%
int num,i=1,j,count;
String ns= request.getParameter("n");
num=Integer.parseInt(ns);
while(i<=num)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
out.println("<font color='blue'>"+i+"</font> is a prime no.<br>");
i++;                             
}                                 
%>
    
           </h2></center>
    </body>
</html>
     
             
Q2. Write a SERVLET program to display the details of Product (ProdCode, PName, Price) on the browser in tabular format. (Use database)      


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
 
public class ProductListServlet extends HttpServlet{
               private Connection con;
               private Statement s;
               private ResultSet rs;
 
               public void doGet(HttpServletRequest req, HttpServletResponse res)
               throws ServletException, IOException{
                               res.setContentType("text/html");
 
                               PrintWriter out = res.getWriter();
 
                               try{
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 
               con = DriverManager.getConnection("jdbc:odbc:mydsn.dsn");
 
                                              s = con.createStatement();
 
                                              rs = s.executeQuery("select * from product");
 
 
                                              out.print("<table border=1 bgcolor='yellow'>"+
                                              "<tr bgcolor='red'>"+
                                              "<th>Code</th><th>Name</th><th>Price</th></tr>");
 
                                              while(rs.next()){
                                                             out.print("<tr>"+
                                                                            "<td>"+rs.getInt(1)+"</td>"+
                                                                            "<td>"+rs.getString(2)+"</td>"+
                                                                            "<td>"+rs.getFloat(3)+"</td>"+
                                                                            "</tr>");
                                              }                                                                                          
                                              out.print("</table>");
                               }
                               catch(Exception e){
                                              out.print(e);
                               }
               }