JSP means Java server page ,it is used to provide dynamic web page to design user-interface
JSP provide HTML Structure by Default If you create JSP under IDE Tools.
We can write JAVA Code in JSP using JSP Expression Tag .
It is called Scriptlet
<%
%>
JSP mostly prefer for designing, only output data we should write on JSP.
IN Netbeans IDE you can add JSP Page to right click on Web Pages
We will Create Web Form using Web Elements to provide User Interface.
Form Design HTML Code in JSP
............................................................................................................................................
Complete Code of JSP to Perform Addition Program:-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>
<form action="" method="post">
<input type="text" name="txtnum1" placeholder="Enter First Number" value="<% if(request.getParameter("txtnum1")!=null){
out.print(request.getParameter("txtnum1"));
} %>" />
<br>
<br>
<input type="text" name="txtnum2" placeholder="Enter Second Number" value="<% if(request.getParameter("txtnum2")!=null){
out.print(request.getParameter("txtnum2"));
} %>" />
<br>
<br>
<input type="submit" name="btnsubmit" value="Click" />
</form>
<%
if(request.getParameter("btnsubmit")!=null)
{
int a = Integer.parseInt(request.getParameter("txtnum1"));
int b = Integer.parseInt(request.getParameter("txtnum2"));
int c = a+b;
out.print("result is "+c);
}
%>
</center>
</body>
</html>
...........................................................................................................................................
<form action="SISer" method="post">
<p>Enter value of P</p>
<p><input type="text" name="txtp" placeholder="enter value of p" /></p>
<p>Enter value of R</p>
<p><input type="text" name="txtr" placeholder="enter value of r" /></p>
<p>Enter value of T</p>
<p><input type="text" name="txtt" placeholder="enter value of t" /></p>
<p><input type="submit" name="btnsubmit" value="Calculate" /></p>
</form>
Complete Servlet Code :-
public class SISer extends HttpServlet {
protected void xyzRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
float p= Float.parseFloat(request.getParameter("txtp"));
float r =Float.parseFloat(request.getParameter("txtr"));
float t=Float.parseFloat(request.getParameter("txtt"));
float si = (p*r*t)/100;
out.print("result is "+si);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
xyzRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
xyzRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
.
POST Answer of Questions and ASK to Doubt