Displaying null page on running my JSP - java

I'm trying to retrieve data from mysql. Upon giving the right input, data is not getting displayed. Code inside my jsp is not working too, but catch block executes correctly. Can you suggest me how to solve this problem?
Here is my code:
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Connection"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.PreparedStatement"%>
<%# page import="java.util.*" %>
<%
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
String url = "jdbc:mysql://localhost:3306/check2allowMultiQueries=true";
%>
<html>
<head>
<link href="cprofile.css" rel="stylesheet" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="view" method="POST">
<%
String z = request.getParameter("code");
String query="Select * from check2 where CODE='"+z+"';";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/check 2", "root", "");
Statement st=con.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
%>
<table border="1">
<tr>
<td><%=rs.getString("NAME")%></td>>
<td><%=rs.getString("MEDIUM")%></td>
<td><%=rs.getString("CODE")%></td>
<td><%=rs.getString("ADD1")%></td>
<td><%=rs.getString("ADD2")%></td>
<td><%=rs.getString("CK")%></td>
<td><%=rs.getString("FK")%></td>
<td><%=rs.getString("BOARD")%></td>
</tr>
</table>
<%}
} catch(Exception e) {
out.println(e);
}
%>

Related

Can not login after register jsp db

I have a problem with a login on my "website" i always have failed login despite this i write good login and password. It gets the value from data base name "register" and check it. And I have values in "register" in database and I normally write it on my login page and something is going wrong cause it redirecting me on "loginfail.jsp"This is my code:
<%# page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<%# page language="java" %>
<%# page import="java.sql.*" %>
<%# page import="java.sql.DriverManager.*" %>
<%
PreparedStatement ps;
Connection conn;
ResultSet rs= null;
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
conn=DriverManager.getConnection("jdbc:derby://localhost:1527/onlineshop","root","root");
Statement st=conn.createStatement();
%>
<%
boolean flag = false;
String LOGIN = request.getParameter ("LOGIN");
String PASSWORD = request.getParameter ("PASSWORD");
%>
<%
String sql = "SELECT LOGIN from REGISTER where LOGIN=? And PASSWORD =?";
try {
ps = conn.prepareStatement(sql);
ps.setString (7,LOGIN);
ps.setString (8,PASSWORD);
rs = ps.executeQuery ();
if (rs.next ()) {
out.println (rs.getString ("LOGIN"));
flag = true;
session.setAttribute("ULOGIN", rs.getString ("LOGIN"));
} else {
request.setAttribute("err", "user name or password error!");
}
rs.close ();
ps.close ();
conn.close ();
} catch (Exception e) {
out.println (e);
}
%>
<%
if (flag) {
%>
<jsp:forward page="loginsucc.jsp" />
<%
}
else {
%>
<jsp:forward page="loginfail.jsp"/>
<%
}
%>
</body>
</html>
The problem in your code is because of using the wrong index in the setString.
Replace
ps.setString (7,LOGIN);
ps.setString (8,PASSWORD);
with
ps.setString (1,LOGIN);
ps.setString (2,PASSWORD);
Check this for an example of using Prepared Statements. Check this for documentation.
[Update]
The rs.next () is returning false in your code. You can validate the following working example:
register.jsp:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<%
boolean flag = true;
if (flag) {
%>
<jsp:forward page="loginsucc.jsp" />
<%
} else {
%>
<jsp:forward page="loginfail.jsp" />
<%
}
%>
</body>
</html>
loginsucc.jsp
<html>
<head>
<title>Insert title here</title>
</head>
<body>
Login successful
</body>
</html>
Output:

how to retrieve data from mysql table while clicking on anchor tag

In my first JSP page my database table rows are displayed in separate articles.
In these articles i have anchor tags which are a column of my table in mysql(Title). I want to click the anchor tag and then in second jsp page i want the row of mysql table to be displayed that has the same Title in the anchor tag.
In my first JSP page, i have an anchor tag under inside try{}catch() that displays Title column from mysql table. i want when i click on it it should go to second jsp page and display the table row of mysql table that has same Title.
Can you help?
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#page import="java.sql.DriverManager" %>
<%#page import="java.sql.ResultSet" %>
<%#page import="java.sql.Statement" %>
<%#page import="java.sql.Connection" %>
<%#page import="java.sql.PreparedStatement" %>
<%
String id = request.getParameter("userid");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception ex){
ex.printStackTrace();
}
Connection con =null;
Statement st = null;
ResultSet rs = null;
%>
<!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">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insert title here</title>
<style type="text/css">
html,
body{
height:100%;
padding:0px;
margin:0px;
background:white;
font-family:'Lora', serif;
min-height:100%;
}
ul{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: white;
width:100%;
border:1px solid grey;
}
li{
float: right;
}
li a{
display: block;
color: #4682B4;
text-align: center;
padding: 20px 22px;
text-decoration: none;
}
li a:hover:not(.active){
background-color: white;
}
header{
font-family:serif;
margin-bottom:0;
background:#4682B4;
height:70px;
color:white;
text-align:center;
name:title;
}
article{
width:300px;
margin-left:auto;
margin-right:auto;
border: 1px solid grey;
float:left;
margin:10px;
}
.image-wrapper{
width:300px;
float:left;
padding-right:25px;
padding-bottom:10px;
}
.image-wrapper img{
width:100%;
}
.article-meta{
font-family:sans-serif;
color:#aaa;
font-size:12px;
}
p{
font-size:20px;
color:#4682B4;
text-decoration:none;
}
a{
font-size:20px;
color:#4682B4;
text-decoration:none;
}
</style>
</head>
<body>
<ul>
<img class="myheader" src="C:\Users\zaryab Ahmad\Desktop\ASIO.jpg" style="width:10%; height:10%">
<li><a class="active" href="NewPost.jsp">NewPost</a></li>
<li>Contact Us</li>
<li>Opportunity</li>
<li>About Us</li>
<li>Projects</li>
<li>Latest News</li>
<li>Home</li>
</ul>
<h1>retreive</h1>
<%
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/asio","root","");
st = con.createStatement();
String query = "Select * from posts";
rs = st.executeQuery(query);
while(rs.next()){
%>
<article>
<header><%=rs.getString("Title") %></header>
<div class="image-wrapper">
<img src="C:\Users\zaryab Ahmad\Desktop\ASIO.jpg"">
<span>Image source : the Web</span>
</div>
<p><%=rs.getString("Title")%>
</p>
<input type="text" name="discription" value="<%=rs.getString("Title")%>">
</article>
<%
}
con.close();
}catch(Exception ex){
ex.printStackTrace();
}
%>
</body>
</html>
this is my second jsp page
in this page i want to display row from mysql table that has a column called Title which is the same in anchor tag in first page. i want the relevent row to be displayed.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#page import="java.sql.DriverManager" %>
<%#page import="java.sql.ResultSet" %>
<%#page import="java.sql.Statement" %>
<%#page import="java.sql.Connection" %>
<%#page import="java.sql.PreparedStatement" %>
<%
String id = request.getParameter("userid");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception ex){
ex.printStackTrace();
}
Connection con =null;
Statement st = null;
ResultSet rs = null;
%>
<!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">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insert title here</title>
<style>
html,
body{
height:100%;
padding:0px;
margin:0px;
background:white;
font-family:'Lora', serif;
min-height:100%;
}
header{
font-family:serif;
margin-bottom:0;
background:#4682B4;
width:100%;
height:70px;
color:white;
text-align:center;
}
</style>
</head>
<body>
<%
try{
String title1 = request.getParameter("discription");
con = DriverManager.getConnection("jdbc:mysql://localhost/asio","root","");
st = con.createStatement();
String query ="Select * from posts where Title='"+title1+"'";
rs = st.executeQuery(query);
while(rs.next()){
%>
<!..i have problem in here..>
<header><%=rs.getString("Title") %></header>
<section class="w3-container w3-center w3-content">
<img class="mySlides" src="C:\Users\zaryab Ahmad\Desktop\asio.jpeg" style="width:100%">
<img class="mySlides" src="C:\Users\zaryab Ahmad\Desktop\asio1.jpg" style="width:100%">
<img class="mySlides" src="C:\Users\zaryab Ahmad\Desktop\wfp.jpg" style="width:100%">
</section>
<script>
var myIndex=0;
carousel();
function carousel(){
var i;
var x = document.getElementsByClassName("mySlides");
//var dots = document.getElementsByClassName("dot");
for(i=0;i<x.length;i++){
x[i].style.display = "none";
}
myIndex++;
if(myIndex > x.length){myIndex=1}
x[myIndex-1].style.display="block";
setTimeout(carousel , 3000);
}
</script>
<p>this is the full paragraph of the post.</p>
<%
}
con.close();
}catch(Exception ex){
ex.printStackTrace();
}
%>
</body>
</html>

My jsp code doesn't insert data in database

I am working on a JSP code and want to connect my page with mysql database and to insert name and email in database but it couldn't , I want to know why
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.*, java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP handle Page</title>
</head>
<body>
<%
String first_name = request.getParameter("user");
String email = request.getParameter("email");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","");
Statement st=conn.createStatement();
int i=st.executeUpdate("insert into info (name,email) values ('"+first_name+"','"+email+"')");
if(i!=0)
{
System.out.println("Data inserted");
}
else
{System.out.println("no");}
}
catch (Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>

I need to create a system where user can login and logout. I am using netbeans and MYSQL database and GlassFish server 4.0

I know it is a simple task but don't able to find out what is the problem.
I made one login.jsp which is starting page.
LoginAction.jsp which will validate the user
I think there is some problem with ReqestDispatcher.
I am trying to find out solution from past 2 days and I am exhausted please help me.
All the table names and their field names are correct.
This is LoginAction.jsp
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="DataBase.DBCONNEction"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.Connection"%>
<%#page import="javax.servlet.RequestDispatcher"%>
<%#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>
<%
String uname = request.getParameter("name");
String pass = request.getParameter("pass");
String unam, upass;
Connection con = null;
PreparedStatement prst = null;
Statement stm = null;
DBCONNEction DBC = null;
con = DBC.getConnection();
stm = con.createStatement();
RequestDispatcher rd = request.getRequestDispatcher("/welcome.jsp");
ResultSet rs = stm.executeQuery("select password from details where username = '" + uname + "'");
if (rs.next()) {
if (rs.getString("password").equals(pass)) { //If valid password
session.setAttribute("User", uname);
}else {
request.setAttribute("Error", "Invalid password.");
rd = request.getRequestDispatcher("/login.jsp");
}
}
else {
request.setAttribute("Error", "Invalid user name.");
rd = request.getRequestDispatcher("/login.jsp");
}
rd.forward(request, response);
%>
</body>
</html>
This is login.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Knowledge Repository System</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<div class="header">
<h1>Knowledge Repository System JSP</h1>
<form id="search" action="loginAction.jsp" method="POST">
<label>Username <input type="text" name="name" id="s-user" style="width:90%;" required></label>
<label>Password<input type="password" name="pass" id="s-pass" style="width:90%;" required></label>
<input type="submit" class="submit" value="Submit">
</form>
</div>
Admin Panel
<div class="nav">
<h2 style="font-size: 36px"><strong>Sign Up</strong></h2>
<form action="details" method="POST">
<signup>
<p><input type="text" name="name" required placeholder="Username"></p>
<p><input type="password" name="pass" required placeholder="password"></p>
<p><input type="text" name="email" required placeholder="xyz#abc.com"></p>
<p><input type="submit" value="Sign Up" align="center"></p>
</signup>
</form>
</div>
</body>
</html>
This is DBCONNEction.java
package DataBase;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBCONNEction {
private static String URL = "jdbc:mysql://localhost:3306/repository";
private static String DRIVER = "com.mysql.jdbc.Driver";
private static String pass = "myserver";
private static String user = "root";
static Connection con = null;
static {
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, user, pass);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return con;
}
}

How to retrieve image from mysql db and show it inside <td> and <img> tag in HTML?

How to retrieve image from mysql db and show it inside tag in HTML and that img tag should be placed inside ? Here s my code:
It displays only the image . Its not showing any content other than image.
Thanks in advance.
<%# 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>
<%# page import="java.io.*"%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%# page import="java.text.*"%>
<%# page import="javax.servlet.*"%>
<%# page import="javax.servlet.http.*"%>
<%# page import="javax.servlet.http.HttpSession"%>
<%# page language="java"%>
<%# page session="true"%>
<%
try{
//PrintWriter out=response.getWriter();
out.println("Retrieve Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "db";
String userName = "root";
String password = "root";
Connection con = null;
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
%>
<table border='1'>
<tr>
<td>Name:</td><td>
</td>My Name</td>
</tr>
<tr>
<td>Image:</td>
<td width=10px;>
<%
PreparedStatement pre1 = con.prepareStatement("select * from image where id="+8);
ResultSet rs1=pre1.executeQuery();
while(rs1.next())
{byte[] bytearray1 = new byte[4096];
int size1=0;
InputStream sImage1;
sImage1 = rs1.getBinaryStream(2);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=logo.jpg");
while((size1=sImage1.read(bytearray1))!= -1 )
{
response.getOutputStream().write(bytearray1,0,size1);
}
response.flushBuffer();
sImage1.close();
rs1.close();
}
out.println("Retrieved Successfully!");
pre.close();
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>
</td></tr>
</table>
</body>
</html>
Images are loaded as separate requests to the request that loaded the html. As such you need to:
establish a URL encoding scheme that you use when specifying the src of the img element.
map a servlet (or similar) to that URL. The servlet loads the image based on the parameters of the URL returning that image in its response.
I found out.
r.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>
<table border=2>
<tr><th>DISPLAYING IMAGE</th></tr>
<tr><td>hi</td></tr>
<tr><td>
<img src="retrieve.jsp" width=130 height=130>
</td></tr>
</table>
</body>
</html>
retrieve.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">
</head>
<body>
<%# page import="java.io.*"%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%# page import="java.text.*"%>
<%# page import="javax.servlet.*"%>
<%# page import="javax.servlet.http.*"%>
<%# page import="javax.servlet.http.HttpSession"%>
<%# page language="java"%>
<%# page session="true"%>
<%
try{
//PrintWriter out=response.getWriter();
out.println("Retrieve Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "db";
String userName = "root";
String password = "root";
Connection con = null;
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
PreparedStatement pre1 = con.prepareStatement("select * from image where id="+8);
ResultSet rs1=pre1.executeQuery();
while(rs1.next())
{byte[] bytearray1 = new byte[4096];
int size1=0;
InputStream sImage1;
sImage1 = rs1.getBinaryStream(2);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=logo.jpg");
while((size1=sImage1.read(bytearray1))!= -1 )
{
response.getOutputStream().write(bytearray1,0,size1);
}
response.flushBuffer();
sImage1.close();
rs1.close();
}
pre.close();
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>
</body>
</html>

Categories

Resources