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

Servelets Notes

The document discusses servlets and how to create them in Java. It includes code examples of servlets that redirect pages, print output, and connect to a database. It also includes HTML pages and a filter class used by the servlets.

Uploaded by

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

Servelets Notes

The document discusses servlets and how to create them in Java. It includes code examples of servlets that redirect pages, print output, and connect to a database. It also includes HTML pages and a filter class used by the servlets.

Uploaded by

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

SERVELETS

18TH JULY

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Override
protected void service(HttpServletRequest req , HttpServletResponse res ) throws
ServletException, IOException
{
res.sendRedirect("page1.html");/*Sends a temporary redirect response to the
client using the specified redirect location URL and clears the buffer*/
}
}
SERVELETS
WEB.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<style>
.position1
{
width:1365px;
height:635px;
}
.position2
{
position:absolute;
top:50%;
left:3%;
}
</style>
<body>
<img src="arjun.jpg" class="position1">
<div class="position2">
<h2 style="color:#D0D3D4; font-family:arial; font-size:25px">Don't
Prove Yourself Everywhere!!</h2>
<h2 style="color:#B9B1B1; font-family:Bradley Hand; font-size:22px">
"Sometimes trying to prove that<br>you are the best is an
insult"</h2>
</div>
</body>
</html>
SERVELETS

20TH JULY

@WebServlet("/Demo")
public class Demo extends HttpServlet
{
int count1=0;
int count2=0;
@Override
public void init() throws ServletException
{
SERVELETS
System.out.println("Init method is called "+ (++count1)+" times");
}

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
{
PrintWriter p =resp.getWriter();//Returns a PrintWriter object that can send
character text to the client
p.println("Service method is called for "+ (++count2)+" times");
}

@Override
public void destroy()
{
System.out.println("Destroy method is called");
}
}
WEB.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello Everyone Very GoodMorning</h1>
</body>
</html>
SERVELETS
21ST & 22nd JULY
SERVELETS
CONNECT.PROPERTIES
GET RESULT.JAVA
public class GetResult extends HttpServlet
{
private static final long serialVersionUID = 1L;

Connection con;
PreparedStatement pstmt;
ResultSet res;
@Override
public void init(){
String path="E:\\March_Feb\\project3\\src\\connect.properties";
try {
FileInputStream fis=new FileInputStream(path);
Properties p=new Properties();
p.load(fis);
String url=p.getProperty("url");
String user=p.getProperty("user");
String password=p.getProperty("password");
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection(url,user,password);
System.out.println("connected");
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
SERVELETS
try{
String usn=req.getParameter("usn");
String sql="SELECT * from details where usn=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1,usn);
res=pstmt.executeQuery();

if(res.next()==true){
String u=res.getString("usn");
String n=res.getString("name");
int m1=res.getInt("sub1");
int m2=res.getInt("sub2");
int m3=res.getInt("sub3");
float avg=res.getFloat("average");

PrintWriter p =resp.getWriter();
p.println("Student USN: "+u);
p.println("Student NAME: "+n);
p.println("Physics Marks: "+m1);
p.println("Mathematics Marks: "+m2);
p.println("Chemistry Marks: "+m3);
p.println("Total Average: "+avg);
}
else{
resp.sendRedirect("error.html");
}
}
catch (SQLException e) {
e.printStackTrace();
}
SERVELETS
}
}
VALIDATION.JAVA
/**
* Servlet Filter implementation class Validation
*/
public class Validation implements Filter {
/**
* Default constructor.
*/
public Validation() {
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException {

String usn=request.getParameter("usn");
if(usn.length()==10){
request.getServletContext().getRequestDispatcher("/GetResult").forward(request,
response);
}
else{
((HttpServletResponse)(response)).sendRedirect("error.html");
}
SERVELETS
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
PCERCENTAGE CALC.JAVA
public class PercentageCal extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws
ServletException, IOException {

PrintWriter p=arg1.getWriter();

HttpSession hs=arg0.getSession();
int m1=(int)hs.getAttribute("mark1");
int m2=(int)hs.getAttribute("mark2");
int m3=(int)hs.getAttribute("mark3");
float per=(((m1+m2+m3)*100)/300);
if(per>=70){
p.println("Result Status :-Distinction");
}
else if(per>=60){
p.println("Result Status :-First Class");
}
else if(per>=50){
p.println("Result Status :-Second Class");
SERVELETS
}
else if(per>=35){
p.println("Result Status :-Just Pass");
}
else{
p.println("Result Status :-Fail");
}
}
}

ERROR.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>
Something went wrong
Check Entered USN format/
Entered USN is INVALID
Sorry Unable proceed further!!!
</h1>
</body>
</html>
INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
SERVELETS
<title>Insert title here</title>
</head>
<style>
p.one
{
padding:60px;
background-color:#2F4F4F;
height:40px;
border-style:solid;
border-width:5px;
}
p.two
{
padding:30px;
background-color:#E9967A;
height:30px;
border-style:solid;
border-width:1px;
}

table,th
{
font_family:arial;
border-collapse:collapse;
width:50%;
}

th
{
border:1px solid #dddddd;
text-align:center;
SERVELETS
padding:8px;
height:70px
}

td{
border:2px solid #dddddd;
text-align:left;
padding:15px;
height:150px
}
tr:nth-child(odd)
{
background-color:#7FED86;
}
</style>
<body>
<center><p class="one" style="font-size:20px; color:white; font-family:Verdana">
<img alt="not found" src="vtu_sym.jpg" width="100" height="90" />
VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELGAVI</p></center>

<center><p class="two" style="font-size:20px; color:white;font-family:Verdana">


VTU PROVISIONAL RESULT
</p>
</center>

<form action="GetResult">
<table align="center">
<tr>
<th>December-2019/January 2020 Examination Result</th>
</tr>
SERVELETS
<tr>
<td>
<label>Enter USN:</label>
<input type="text" name="usn" />
<br><br><br>
<input type="submit" value="submit" />
</td>
</tr>

</table>
</form>
</body>
</html>

You might also like