Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

JSP Programs

 JSP Programs

Java JSP

1) Write a JSP program to accept number and display table and factorial of number

Html

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form action=Factorial.jsp>

<pre>

Enter any number      <input type = text name = t1>

<input type = submit value = Submit> <input type = reset value = reset>

</pre>

</body>

</html>

 

jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%

int n1 = Integer.parseInt(request.getParameter("t1"));

 

out.print("Table of "+n1+" : ");

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

out.println(n1+" * "+i+" = "+(n1*i)+"<br>");

}

int a=1;

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

a=a*i;

}

out.println("<h1>The Factorial of "+n1+" is "+a);

%>

</body>

</html>


 

2) Write a JSP page insert records employee(eno, name, dept,sal). Also add another JSP page for displaying all the employees.

Code-

d2.html

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form action=D2first.jsp method=post>

<pre>

eno <input type=text name = t1>

name <input type=text name = t2>

dept <input type=text name = t3>

sal <input type=text name = t4>

<input type = submit value = Submit> <input type = reset value = reset>

</pre>

</form>

</body>

</html>

 

D2input.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<%@ page import = "java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%

int n1 = Integer.parseInt(request.getParameter("t1"));

String n2 = request.getParameter("t2");

String n3 = request.getParameter("t3");

int n4 = Integer.parseInt(request.getParameter("t4"));

try {

Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/catD","root","8520");

System.out.println("Connection created");

PreparedStatement ps = con.prepareStatement("insert into emp values(?,?,?,?)");

ps.setInt(1, n1);

ps.setString(2, n2);

ps.setString(3, n3);

ps.setInt(4,n4);

ps.executeUpdate();

}

catch(Exception e) {

System.out.println(e);

 

 

}

%>

<form action=d2show.jsp >

<pre>

<h1>Table Created

<input type = submit value = Show>

</pre>

</form>

</body>

</html>

 

D2show.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

    <%@ page import = "java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%

 

 

try {

Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/catD","root","8520");

System.out.println("Connection created");

Statement st = con.createStatement();

String sql = "select * from emp";

ResultSet rs = st.executeQuery(sql);

ResultSetMetaData rsmd = rs.getMetaData();

int cols =rsmd.getColumnCount();

out.println("<table border = 2><tr>");

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

out.println("<th>"+rsmd.getColumnName(i)+"</td>");

}

out.print("</tr>");

while(rs.next()) {

out.print("<tr> ");

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

out.print("<td>"+rs.getString(i)+"</td>");

}

out.println("</tr>");

}

out.println("</table>");

}

catch(Exception e) {

System.out.println(e);

 

 

}

 

%>

</body>

</html>

</body>

</html>



JSP Programs JSP Programs Reviewed by Asst. Prof. Sunita Rai on March 27, 2022 Rating: 5

No comments:

Powered by Blogger.