Hello again:)
I have a problem.
What iam trying to do :
page where user can login/create account if dont have , if logined in success page is appearing where he can add his blood glucose from tooday and also he can check his blood glucose from previous days in chart.
let me show you my code:
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Mateusz Baran - jsp </title>
</head>
<body>
<input type=button onClick="parent.location='reg.html'" value='Reg'>
<input type=button onClick="parent.location='login.jsp'" value='Login'>
</body>
</html>
reg.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String sUserID=request.getParameter("sUserID");
session.setAttribute("sUserID",sUserID);
String sPassword=request.getParameter("sPassword");
String sFirstName=request.getParameter("sFirstName");
String sLastName=request.getParameter("sLastName");
String sEmail=request.getParameter("sEmail");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tabela","root", "root");
Statement st= con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into usermaster (sUserID, sPassword, sFirstName, sLastName, sEmail) VALUES"
+ " ('"+sUserID+"','"+sPassword+"','"+sFirstName+"','"+sLastName+"','"+sEmail+"')");
int b=st.executeUpdate("CREATE TABLE "+sUserID+" (nr_badania int, wynik_badania int)");
String message="Zarejestrowano uzytkownika pomyslnie" +"<br>"+"<br>"+"Zaloguj sie:";
response.sendRedirect("login.jsp?error="+message+"<br>");
%>
success.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java"%>
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="../WEB-INF/lib/amcharts.js" type="text/javascript"></script>
<title>Successfully Login by JSP</title>
</head>
<body>
<input type=button onClick="parent.location='logOut.jsp'" value='Wyloguj'>
<br>
you have been logged in successfully<br />
your data:<br />
<%
out.print("username :<b>"+session.getAttribute("sUserID")+ "</b>"+"<br>");
out.print("name and surnam :<b>"+session.getAttribute("sUserName")+"</b>"+"<br>");
out.print("E-mail :<b>"+session.getAttribute("sEmail")+"</b>");
%>
<form action="nowe_dane.jsp" method="post">
<input type="text" name="wynik_badania" /> Blood glucose from tooday:
<script language="javascript">
<!--
today = new Date();
document.write(" ", today.getDate(),"/",today.getMonth()+1,"/",today.getYear());
//-->
</script> <br>
<input type=button onClick="window.parent.location.href = 'servlets/servlet/PieChartServlet' " value='chart'>
<input type="submit" />
</body>
</html>
nowe_dane.jsp
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String wynik_badania=request.getParameter("wynik_badania");
session.setAttribute("wynik_badania",wynik_badania);
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tabela","root", "root");
Statement st= con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into "+session.getAttribute("sUserID")+" (wynik_badania) VALUES ("+wynik_badania+")");
%>
doLogin.jsp
<%@ page language="java" import="java.sql.*" errorPage="" %>
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tabela","root", "root");
ResultSet rsdoLogin = null;
PreparedStatement psdoLogin=null;
String sUserID=request.getParameter("sUserName");
String sPassword=request.getParameter("sPwd");
String sEmail=request.getParameter("sEmail");
String message="Zalogowano pomyslnie ";
try{
String sqlOption="SELECT * FROM usermaster where sUserID=? and sPassword=?";
psdoLogin=conn.prepareStatement(sqlOption);
psdoLogin.setString(1,sUserID);
psdoLogin.setString(2,sPassword);
rsdoLogin=psdoLogin.executeQuery();
if(rsdoLogin.next())
{
String sUserName=rsdoLogin.getString("sFirstName")+" "+rsdoLogin.getString("sLastName");
session.setAttribute("sUserID",rsdoLogin.getString("sUserID"));
session.setAttribute("sUserName",sUserName);
session.setAttribute("sEmail",rsdoLogin.getString("sEmail"));
response.sendRedirect("success.jsp?error="+message);
}
else
{
message="Nazwa uzytkowanika badz haslo bledne" ;
response.sendRedirect("login.jsp?error="+message);
}
}
catch(Exception e)
{
e.printStackTrace();
}
/// close object and connection
try{
if(psdoLogin!=null){
psdoLogin.close();
}
if(rsdoLogin!=null){
rsdoLogin.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
doLogOut.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<head>
<title>Mateusz Baran - jsp</title>
<script>
function menuReg()
{
document.location.href("reg.html");
}
function menuLogowanie()
{
document.location.href("login.jsp");
}
function menuIndex()
{
document.location.href("index.jsp");
}
</script>
</head>
<body>
<input type=button onClick="parent.location='index.jsp'" value='Index'>
<input type=button onClick="parent.location='reg.html'" value='Rejestracja'>
<input type=button onClick="parent.location='login.jsp'" value='Logowanie'>
<br>
<br>
<br>
<%session.invalidate();%>
Zostales wylogowany.
</body>
</html>
login.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String error=request.getParameter("error");
if(error==null || error=="null"){
error="";
}
%>
<html>
<head>
<title>Mateusz Baran - jsp</title>
</head>
<body>
<div><%=error%></div>
<input type=button onClick="parent.location='reg.html'" value='Rejestracja'>
<input type=button onClick="parent.location='index.jsp'" value='Index'>
<br>
<br>
<br>
<form name="frmLogin" onSubmit="return validate();" action="doLogin.jsp" method="post">
<input type="text" name="sUserName" />Username <br />
<input type="password" name="sPwd" />Password<br />
<input type="submit" name="sSubmit" value="Zaloguj" />
</form>
</body>
</html>
PieChartServlet.jsp
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.jdbc.JDBCCategoryDataset;
/* Code for the HTTP Servlet that will return the Pie Chart as a PNG image
back to the browser after generating it using JFreeChart API */
public class PieChartServlet extends HttpServlet {
public PieChartServlet() {
/* No code in the constructor for this demonstration */
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
javax.servlet.http.HttpSession session = request.getSession();
session.setAttribute("sUserID","sUserID");
String sUserID = (String) session.getAttribute("sUserID");
OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
try {
String query = "SELECT nr_badania, wynik_badania from "+sUserID+" ";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(
"jdbc:mysql://localhost:3306/tabela", "com.mysql.jdbc.Driver", "root", "root");
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createLineChart("Badanie Poziomu Cukru", "Numer Badania", "Poziom cukru",dataset, PlotOrientation.VERTICAL, true, true, false);
response.setContentType("image/png"); /* Set the HTTP Response Type */
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);/* Write the data to the output stream */
}
catch (Exception e) {
System.err.println(e.toString()); /* Throw exceptions to log files */
}
finally {
out.close();/* Close the output stream */
}
}
}
I think that my mistake is in PieChartServlet.jsp because when iam using some static username in line
` String query = "SELECT nr_badania, wynik_badania from "+sUserID+" ";`
for example like this:
String query = "SELECT nr_badania, wynik_badania from User ";
All works fine , i just have problem with getting username from session and insert it into sql query.
So any one could tell me how to tell my servlet what username is keeped in session?