Ronayne
asked on
amend jsp code
Hi, im using the below commands to set a name a user has entered in a jsp bean. Id like to amend this so the the users id is set instead.
<jsp:useBean id="AccessV" scope="session" class="classfiles.AccessVa
<% AccessV.setName(request.ge
Ive been trying this:
string name = request.getParameter("strU
<% AccessV.setName(select idnum from users where name like 'name'); %>
That does'nt work although.
Thanks
<% AccessV.setName("select idnum from users where name like '" + name + "'"); %>
ASKER
I tried that but got this error:
Generated servlet error:
[javac] Compiling 1 source file
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localh
symbol : variable name
location: class org.apache.jsp.insert_jsp
AccessV.setName("select idnum from users where name like '" + name + "'");
<%
String name = request.getParameter("strU serName");
AccessV.setName("select idnum from users where name like '" + name + "'");
%>
String name = request.getParameter("strU
AccessV.setName("select idnum from users where name like '" + name + "'");
%>
ASKER
Ive tried that as well but it just outputs all my insert.jsp source code.
This is insert.jsp:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="classfiles.*" %>
<jsp:useBean id="AccessV" scope="session" class="classfiles.AccessVa
<%
String name = request.getParameter("strU
AccessV.setName("select idnum from users where name like '" + name + "'");
%>
try {
String userName = request.getParameter("strU
String pass = request.getParameter("pass
if (userName != null)
out.print("User Name: " +userName);
String inServer = "localhost";
String inDBName = "test";
String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
Class.forName("com.mysql.j
Connection con = DriverManager.getConnectio
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("select password from userdesc where password like \""+pass+"\" and name like \""+name+"\"");
String passwordtocompare = "";
if (rs1.next())
{
passwordtocompare = rs1.getString(1);
out.print("\nCorrect password");
response.sendRedirect("mai
Object id = getServletConfig().getServ
boolean loggedIn = (id != null);
}
else {
out.print("\nIncorrect username / password");
response.sendRedirect("ree
}
// out.print("\nPassword:" +passwordtocompare);
//if (passwordtocompare.equals(
// out.print("\nCorrect password");
// }
/*else {
out.print("\nIncorrect password");
}*/
}
catch (ClassNotFoundException cnfe) {
out.println(cnfe.toString(
}
catch (SQLException sqle) {
out.println(sqle.toString(
}
%>
ASKER
Ive fixed that but its now just outputting :select idnum from users where name like 'adrian_eire' on the page it links to
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How can I do this, this is my bean that contains the setName method
package classfiles;
public class AccessVals {
private String name;
public AccessVals() {
name = "";
}
public String getName() {
return name;
}
public void setName(String x) {
name = x;
}
}
package classfiles;
public class AccessVals {
private String name;
public AccessVals() {
name = "";
}
public String getName() {
return name;
}
public void setName(String x) {
name = x;
}
}
public void setName(String x) {
Class.forName("com.mysql.j dbc.Driver ").newInst ance();
Connection con = DriverManager.getConnectio n(DATABASE _URL, "username", "password");
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("select idnum from users where name like '" + x + "' );
if (rs.next()) {
name = rs1.getString("idnum");
}
rs.close();
stmt.close();
con.close();
}
then call setName like this:
<%
String name = request.getParameter("strU serName");
AccessV.setName(name);
%>
Class.forName("com.mysql.j
Connection con = DriverManager.getConnectio
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("select idnum from users where name like '" + x + "' );
if (rs.next()) {
name = rs1.getString("idnum");
}
rs.close();
stmt.close();
con.close();
}
then call setName like this:
<%
String name = request.getParameter("strU
AccessV.setName(name);
%>
ASKER
I amedned as you said but its still causing this error: <<Ive fixed that but its now just outputting :select idnum from users where name like 'adrian_eire' on the page it links to
This is the jsp along with the bean (i know its skimply coded but it will do for now)
<%
String name = request.getParameter("strU serName");
AccessV.setName(name);
%>
package classfiles;
import java.sql.*;
import java.util.*;
public class AccessVals {
private String name;
public AccessVals() {
name = "";
}
public String getName() {
return name;
}
public void setName(String x) {
try{
String inServer = "localhost";
String inDBName = "test";
String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
Class.forName("com.mysql.j dbc.Driver ").newInst ance();
Connection con = DriverManager.getConnectio n(DATABASE _URL, "username", "password");
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("select idnum from users where name like '" + x + "'" );
if (rs1.next()) {
name = rs1.getString("idnum");
}
rs1.close();
stmt.close();
con.close();
}
catch(ClassNotFoundExcepti on e){
}
catch(InstantiationExcepti on ee){
}
catch(IllegalAccessExcepti on eee){
}
catch(SQLException eeee){
}
}
//public void setName(String x) {
//name = x;
// }
}
This is the jsp along with the bean (i know its skimply coded but it will do for now)
<%
String name = request.getParameter("strU
AccessV.setName(name);
%>
package classfiles;
import java.sql.*;
import java.util.*;
public class AccessVals {
private String name;
public AccessVals() {
name = "";
}
public String getName() {
return name;
}
public void setName(String x) {
try{
String inServer = "localhost";
String inDBName = "test";
String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
Class.forName("com.mysql.j
Connection con = DriverManager.getConnectio
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("select idnum from users where name like '" + x + "'" );
if (rs1.next()) {
name = rs1.getString("idnum");
}
rs1.close();
stmt.close();
con.close();
}
catch(ClassNotFoundExcepti
}
catch(InstantiationExcepti
}
catch(IllegalAccessExcepti
}
catch(SQLException eeee){
}
}
//public void setName(String x) {
//name = x;
// }
}
ASKER
Its ok, I fixed that, should'nt this output the value : <%=AccessV.getName()%>
yeah... you should be able to do that.
ASKER
That wont work, ive tried this also :
String n = AccessV.getName();
out.print(n);
But nothing is displayed in both cases