I'm new to Java and I'm trying to create a project in myEclipse latest version and I'm using the following code:
<%# page language="java" import="java.util.*,java.sql.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'blockmails.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
try
{
String flag="false";
String ctr="false";
String mailto=session.getAttribute("mailto").toString();
String sub=session.getAttribute("subject").toString();
String mess=session.getAttribute("message").toString();
String user=request.getParameter("user");
System.out.println(mailto+" "+sub+" "+mess+" "+user);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","email","email");
Statement pst=con.createStatement();
Statement pst1=con.createStatement();
//System.out.println("pst1:"+pst1);
//System.out.println("pst2:"+pst2);
Statement st=con.createStatement();
Statement srs=con.createStatement();
Statement st3=con.createStatement();
//out.println("connection estblished");
ResultSet rs=pst.executeQuery("select * from Encryptedwords");
ResultSet rs2=st3.executeQuery("select * from swords");
while(rs.next()&& rs2.next())
{
String query="select * from MAILS where subject like '%"+rs.getString(1)+"%' or subject like '%"+rs2.getString(1)+"%' or message like '%"+rs.getString(1)+"%' or message like '%"+rs2.getString(1)+"%' and mailfrom='"+user+"'";
ResultSet rs1=st.executeQuery(query);
ResultSet rsr1=srs.executeQuery("select * from MAILS where subject like '%"+rs.getString(2)+"%' or message like '%"+rs.getString(2)+"%' and mailfrom='"+user+"'");
if(rs1.next()||rsr1.next())
{
flag="true";
break;
}
else
flag="false";
}
if(flag.equals("true"))
{
//out.println("suspicious");
String to=user;
String mailfrom="GNITS#suspicious mail.com";
String subject="mail-daemon";
String message="The mail you had sent consists of suspicious information";
PreparedStatement pst2=con.prepareStatement("insert into inbox values(?,?,?,?)");
pst2.setString(1,to);
pst2.setString(2,mailfrom);
pst2.setString(3,subject);
pst2.setString(4,message);
int d=pst2.executeUpdate();
PreparedStatement pst3=con.prepareStatement("insert into blocklist values(?,?,?,?)");
pst3.setString(1,mailto);
pst3.setString(2,user);
pst3.setString(3,sub);
pst3.setString(4,mess);
int r=pst3.executeUpdate();
System.out.println("r is:"+d);
if(d>0)
{
Statement st2=con.createStatement();
st2.executeUpdate("delete from mails");
response.sendRedirect("inbox.jsp?user="+to);
}
}
else
{
Statement pss=con.createStatement();
ResultSet sr=pss.executeQuery("select * from alertwords");
//out.println(sr);
while(sr.next())
{
Statement ss=con.createStatement();
String query2="select * from MAILS where subject like '%"+sr.getString(1)+"%' or message like '%"+sr.getString(1)+"%' and mailfrom='"+user+"'";
out.println("hello");
out.println("query:"+query2);
ResultSet sr1=ss.executeQuery(query2);
out.println(sr1);
if(sr1.next())
{
ctr="true";
break;
}
else
ctr="false";
}
if(ctr.equals("true"))
{
/*System.out.println("suspicious");
String to=user;
String mailfrom="krest#gmail.com";
String subject="mail-daemon";
String message="The mail you had sent consists of suspicious information";*/
/*PreparedStatement pss1=con.prepareStatement("insert into inbox values(?,?,?,?)");
pss1.setString(1,mailto);
pss1.setString(2,mailfrom);
pss1.setString(3,subject);
pss1.setString(4,message);
int d1=pss1.executeUpdate();*/
PreparedStatement pss4=con.prepareStatement("insert into inbox values(?,?,?,?)");
pss4.setString(1,mailto);
pss4.setString(2,user);
pss4.setString(3,sub);
pss4.setString(4,mess);
int rk=pss4.executeUpdate();
System.out.println("r is:"+rk);
PreparedStatement pss3=con.prepareStatement("insert into alertlist values(?,?,?,?)");
pss3.setString(1,mailto);
pss3.setString(2,user);
pss3.setString(3,sub);
pss3.setString(4,mess);
int r1=pss3.executeUpdate();
/*System.out.println("r is:"+d1);
if(d1>0)
{
Statement st3=con.createStatement();
st3.executeUpdate("delete from mails");
response.sendRedirect("inbox.jsp?user="+to);
}*/
}
else
System.out.println("not suspicious");
PreparedStatement pss2=con.prepareStatement("insert into inbox values(?,?,?,?)");
pss2.setString(1,mailto);
pss2.setString(2,user);
pss2.setString(3,sub);
pss2.setString(4,mess);
int r=pss2.executeUpdate();
System.out.println("r is:"+r);
if(r>0)
{
Statement st4=con.createStatement();
st4.executeUpdate("delete from mails");
response.sendRedirect("user.jsp?user="+user);
//out.println("mail sent successfully");
}
}
}
catch(Exception e){}
%>
</html>
I'm trying to create a JSP page. But the problem is, when I try to run the code, it gives a blank page. No errors or anything. Just blank. I'm very confused and I have been at this for weeks now, with no progress at all. Any help would be greatly appreciated.
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:
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'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);
}
%>
Here is the Servlet
public class displayServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Connection con = dbConnection.getConnection();
String query = "SELECT theaterNames, FROM thtrname where id in(11,12)";
try{
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
String str = null;
while(rs.next()){
str = rs.getString(1);
}
out.print(str);
HttpSession session = request.getSession();
session.setAttribute("thtr", str);
RequestDispatcher rd = request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
rs.close();
ps.close();
con.close();
}catch(Exception ex){
}
}
}
Here is the success.jsp
<%#page contentType="text/html" pageEncoding="UTF-8" session="true"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<font color="green">
<%= request.getAttribute("thtr") %>
<h1><c:out value="${sessionScope.thtr}" /></h1>
</font>
</body>
</html>
I just don't get it where I am going wrong. I am using both JSTL and Scriptlet but all I am getting on the success.jsp page is null.
You need to set the value to request instead of session
from
session.setAttribute("thtr", str);
to
request.setAttribute("thtr", str);
a then to show just use ${thtr} in jsp