You are on page 1of 13

Gadre Shlok S.

Advance Java TY IT-1


160410116031 BATCH-B

Output :-

SVIT-VASAD 77 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

Open Ended Program

Aim: “Student blog and online forum”

(1)index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="forLogin">
name : <input type="text" name="name"><br><br>
password : <input type="text" name="id"><br><br>
<input type="submit" name="sub" value="login"><br><br>
<a href="signup.jsp">create new account?</a><br><br>
<a href="display.jsp">blogs</a><br>
</form>
</body>

</html>

OUTPUT:

SVIT-VASAD 78 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

(2)signup.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="forLogin">
name : <input type="text" name="name"><br><br>
college : <input type="text" name="college"><br><br>
<input type="radio" name="rad" value="student">Student
<input type="radio" name="rad" value="teacher">Teacher<br><br>
email : <input type="text" name="email"><br><br>
<input type="submit" name="sub" value="signup"><br><br>
</form>
</body>

</html>

OUTPUT:

(3)blog.php
<%@page import="java.io.PrintWriter"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

SVIT-VASAD 79 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
HttpSession hs=request.getSession();
String isValid=(String)hs.getAttribute("user");
if(isValid==null)
{
request.getRequestDispatcher("/index.html").forward(request,
response);
}
else
{
String name=request.getParameter("name");
out.println("<h1>welcome "+name+"</h1><br><br>");
int _sno[]=new int[100];
int _id[]=new int[100];
int _lik[]=new int[100];
int _dik[]=new int[100];
String _topic[]=new String[100];
String _para[]=new String[100];
String _name[]=new String[100];
String _post[]=new String[100];
String _comment[]=new String[100];
int size=0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/stddb","root","");
Statement st=con.createStatement();
ResultSet rs1=st.executeQuery("select * from blog");
int i=0;
while(rs1.next())
{
_sno[i]=rs1.getInt(1);
_id[i]=rs1.getInt(2);
_topic[i]=rs1.getString(3);
_para[i]=rs1.getString(4);
i++;
size++;
}
i=0;

for(int j=0;j<size;j++)
{
ResultSet rs2=st.executeQuery("select * from user
where id="+_id[j]);
while(rs2.next())
{
_name[j]=rs2.getString("name");
_post[j]=rs2.getString("post");
}
rs2.close();

i=0;
ResultSet rs3=st.executeQuery("select * from review");

SVIT-VASAD 80 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B
while(rs3.next())
{
_lik[i]=rs3.getInt("lik");
_dik[i]=rs3.getInt("dik");
i++;
}

rs1.close();
rs3.close();
st.close();
con.close();
}
catch(Exception e)
{
System.out.println("LoginServlet error :
"+e.getMessage());
}
%>
<table border="1">
<tr>
<th>topic no.</th>
<th>topic</th>
<th>name</th>
<th>user id</th>
<th>post</th>
<th>blog</th>
<th>likes</th>
<th>dislikes</th>
</tr>
<%
for(int j=0;j<size;j++)
{
out.print("<tr>");

out.print("<td>");
out.println(_sno[j]);
out.print("</td>");

out.print("<td>");
out.println(_topic[j]);
out.print("</td>");

out.print("<td>");
out.println(_name[j]);
out.print("</td>");

out.print("<td>");
out.println(_id[j]);
out.print("</td>");

out.print("<td>");
out.println(_post[j]);
out.print("</td>");

out.print("<td>");
out.println(_para[j]);
out.print("</td>");

out.print("<td>");
out.println(_lik[j]);
out.print("</td>");

SVIT-VASAD 81 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

out.print("<td>");
out.println(_dik[j]);
out.print("</td>");

out.print("</tr>");
}
%>
</table>
<br><br>
<h1>Give your review</h1>
<form action="forReview">
<h4>
topic on. : <input type="text" name="sno"><br><br>
id : <input type="text" name="id" ><br><br>
<input type="radio" name="rad" value="like">Like
<input type="radio" name="rad" value="dislike">Dislike<br><br>
comment : <input type="text" name="comm"><br><br>
<input type="submit" name="sub1" value="submit"><br>
<a href="newblog.jsp">enter your blog</a>
</h4>
</form>

<%
}
%>
</body>

</html>

OUTPUT:

SVIT-VASAD 82 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

(4)newblog.php

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


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="forReview">
topic : <input type="text" name="topic"><br><br>
id : <input type="text" name="id"><br><br>
blog : <input type="text" name="para"><br><br>
<input type="submit" name="sub1" value="publish">
</form>
</body>
</html>

OUTPUT:

(5)LoginServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

SVIT-VASAD 83 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

import java.sql.Statement;

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

@WebServlet("/forLogin")
public class LoginServlet extends HttpServlet
{
protected void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
String acc=request.getParameter("sub");
if(acc.matches("login"))
{
int id=Integer.parseInt(request.getParameter("id"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/stddb","root","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from user");
while(rs.next())
{
if(id==rs.getInt(1))
{
HttpSession hs=request.getSession();
hs.setAttribute("user", "logged");

request.getRequestDispatcher("/blog.jsp").forward(request, response);
}
}

st.close();
con.close();
}
catch(Exception e)

SVIT-VASAD 84 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

{
System.out.println("LoginServlet error :
"+e.getMessage());
}
}
else if(acc.matches("signup"))
{
HttpSession hs=request.getSession();
hs.setAttribute("user", "logged");
String name=request.getParameter("name");
String college=request.getParameter("college");
String post=request.getParameter("rad");
String email=request.getParameter("email");

try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/stddb","root","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from user");
int i=0;
while(rs.next())
{
i++;
}
st.executeUpdate("insert into user
values("+(++i)+",'"+name+"','"+college+"','"+post+"','"+email+"')");

request.getRequestDispatcher("/blog.jsp").forward(request, response);

st.close();
con.close();
}
catch(Exception e)
{
System.out.println("LoginServlet error :
"+e.getMessage());
}
}

SVIT-VASAD 85 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

else
{

}
}

(6)ReviewServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

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("/forReview")
public class ReviewServlet extends HttpServlet
{
protected void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{

String click=request.getParameter("sub1");
if(click.matches("submit"))
{
int sno=Integer.parseInt(request.getParameter("sno"));
int id=Integer.parseInt(request.getParameter("id"));
String rev=request.getParameter("rad");
String comment=request.getParameter("comm");
int count1=0,count2=0;

SVIT-VASAD 86 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/stddb","root","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from
review where sno="+sno);
while(rs.next())
{
count1=rs.getInt("lik");
count2=rs.getInt("dik");
}
}
catch(Exception e)
{
System.out.println("LoginServlet error :
"+e.getMessage());
}
if(rev.matches("like"))
{
count1++;
}
else if(rev.matches("dislike"))
{
count2++;
}
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/stddb","root","");
Statement st=con.createStatement();
st.executeUpdate("update review set
lik="+count1+",dik="+count2+" where sno="+sno+" and id="+id);

st.executeUpdate("insert into comment


values("+sno+","+id+",'"+comment+"')");
st.close();
con.close();
}
catch(Exception e)

SVIT-VASAD 87 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

{
System.out.println("LoginServlet error :
"+e.getMessage());
}
request.getRequestDispatcher("/blog.jsp").forward(request,
response);
}

else if(click.matches("publish"))
{
String topic=request.getParameter("topic");
int id=Integer.parseInt(request.getParameter("id"));
String blog=request.getParameter("para");

try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/stddb","root","");
Statement st=con.createStatement();

ResultSet rs=st.executeQuery("select * from blog");


int i=0;
while(rs.next())
{
i++;
}
st.executeUpdate("insert into blog
values("+(++i)+","+id+",'"+topic+"','"+blog+"')");
st.executeUpdate("insert into review
values("+i+","+id+",0,0)");
st.close();
con.close();
}
catch(Exception e)
{
System.out.println("LoginServlet error :
"+e.getMessage());
}
request.getRequestDispatcher("/blog.jsp").forward(request,
response);

SVIT-VASAD 88 | P a g e
Gadre Shlok S. Advance Java TY IT-1
160410116031 BATCH-B

}
}

SVIT-VASAD 89 | P a g e

You might also like