I am trying to display the user ID from a database in a textbox. In the input I am trying to retrieve the user ID, which works, but when I try display it in a textbox it does not appear? The java part if my code is working but I am unsure how I can display the results set in a textbox? Any help much appreciated.
<!DOCTYPE html>
<html>
<head>
<title>JPage</title>
</head>
<body>
<%
try{
String session_id =null;
HttpSession session1=request.getSession(false);
if(session1!=null){
session_id=(String)session1.getAttribute("name");
}
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/alt", "root", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from register where uid='"+session_id+"'");
rs.next();
String uid = rs.getString("uid");
%>
<form action="register_1.jsp" method="post">
<input type="text"name="TestType" value="Chemical">
<imput type="text" value=" <%out.print(uid);%> " name="uid" >
<input value="Create" type="submit" class="btn" style="width: 100px;">
</form>
<%
}catch(Exception e){
out.println(e);
}
%>
</body>
</html>
`
Related
this is my jsp page.where im displaying a form and sending the values to another servlet page to insert the data into mysql database.im using post method to send all values.servlet name is updateVehicle2
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Modify Vehicle</title>
</head>
<center><h1> Modify Vehicle</h1></center>
<center>
<form action="updateVehicle2" method="Post">
<label >Enter Vehicle name:</label>
<input type="text" name="vname" required><br><br>
<label >Enter Vehicle model:</label>
<input type="text" name="vmodel" required ><br><br>
<label >Enter Vehicle ID:</label>
<input type="text" name="vid" required ><br><br>
<label >Select Vehicle type:</label>
<select name="vtype" required>
<option disabled selected>select type</option>
<option value="Ac">Ac</option>
<option value="NonAc">Non Ac</option>
</select><br><br>
<label >Rate per hour:</label>
<input type="text" name="vrph" required ><br><br>
<label >Rate per km:</label>
<input type="text" name="vrpk" required ><br><br>
<input type="submit" value="Add Vehicles">
</form></center>
this is my servlet page.here im trying to retrieve values by using request.getParameter() ,but im getting null values here.every values is null.i have used name attribute for retrieving values .but,still it returning null values.please help me struggling with this problem morethan 5 hours.thanks in advance
String vn=request.getParameter("vname");
String vmodel=request.getParameter("vmodel");
String vid=request.getParameter("vid");
String vtype=request.getParameter("vtype");
String vrph=request.getParameter("vrph");
String vrpk=request.getParameter("vrpk");
response.setContentType("text/html");
final String JDBC_DRIVER="com.mysql.jdbc.Driver";
final String DB_URL="jdbc:mysql://localhost:3306/startrek";
final String user="root";
final String pass="kingmaker";
Connection conn=null;
PreparedStatement stmt;
stmt = null;
int numRows=0;
PrintWriter out = response.getWriter();
out.println(vn);
out.println(vmodel);
try
{
Class.forName(JDBC_DRIVER);
conn=DriverManager.getConnection(DB_URL,user,pass);
stmt = conn.prepareStatement("update vehicles set vname=?,vmodel=?,vtype=?,vrph=?,vrpk=? where vid=?;");
stmt.setString(1,vn);
stmt.setString(2,vmodel);
stmt.setString(3,vtype);
stmt.setString(4,vrph);
stmt.setString(5,vrpk);
stmt.setString(6,vid);
numRows=stmt.executeUpdate();
if(numRows>0)
{
out.println("updated successfully");
}
else
{
out.println("not succesfully");
}
}
catch(Exception e)
{
out.println(e);
}
}
}
To overcome from this problem write '#MultipartConfig' before your Servlet class.
like-->
#MultipartConfig
public class EditUserServlet extends HttpServlet {}
I am using hidden input type to set value and using request.getParameter()
to get the value in another page but it is always returning “null”.
Why it is returning null value?
First Page:-
<%
try{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/passkey_database","postgres","4457");
PreparedStatement ps=con.prepareStatement("select * from visitor_table");
ResultSet rs=ps.executeQuery();
ResultSetMetaData metaData=rs.getMetaData();
while(rs.next()){
%>
<tr>
<td>
<input type="text" name="name" value="<%=rs.getString("visitor_name")%>">
<%=rs.getString("visitor_name")%>
</td>
<%for(int i = 2; i<=metaData.getColumnCount();i++){ %>
<td>
<%= rs.getString(i)%>
</td>
<% } %>
</tr>
<%}
}catch (Exception e) {
e.printStackTrace();
}
Second Page:-
<%
String name=request.getParameter("name");
out.println(name);
try{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/passkey_database","postgres","4457");
PreparedStatement ps=con.prepareStatement("select * from visitor_table where visitor_name=?");
ps.setString(1,name);
ResultSet rs=ps.executeQuery();
while(rs.next()){
%>
You can do this task by just a simple trick
update this line
<%
String visitor_name = rs.getString("visitor_name");
String link = "permissionform.jsp?name=" + visitor_name;
%>
<input type="text" name="name" value="<%=visitor_name%>">
<%= visitor_name%>
by this code you really dont need the <input> tag if you use this code because link is already parsed.
by this name value forworded will not be null if visitor_name is not null
Hope it helps your problem.:)
You can use Session , Cookie or Context objects to get variable in other page,or use form tag with action another page if you have <input type="submit"/> tag
try this code
<td>
<form action="permissionForm.jsp">
<input type="text" name="name"
value="<%=rs.getString("visitor_name")%>">
<input type="submit" value="<%=rs.getString("visitor_name")%>"/>
</form>
</td>
i ill created the program of create, insert, update, delete, exit using in jsp and mysql only. i got the solutions for insert, delete, exit, and create but, update section only i got error., in my DB name is employee and table name is employees, the fields are in the order like 1. employee name, 2. employee_id, 3. nationality,. In update part i'm using the Employee_id is displayed in default, it's also displayed but i got error in 3 field of Nationality. I send my code if you any clear my error,. thanks in advance. the coding following as:
<%# page import="java.sql.*" %>
<%# page import="java.sql.DriverManager.*" %>
<%# page import="java.sql.Statement.*" %>
<%# page import="java.io.*" %>
<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>
<form action="editEmployee.jsp" name="UpdateForm" method="post">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality"></td>
</tr>
<%
String EmployeeName=request.getParameter("EmployeeName");
String Nationality=request.getParameter("Nationality");
PreparedStatement ps;
Connection con=null;
String driverName="com.mysql.jdbc.Driver";
String connectionUrl="jdbc:mysql://localhost:3306/";
String user="root";
String password="sarakrish";
String dbName="employee";
try{
Class.forName(driverName);
con=DriverManager.getConnection(connectionUrl+dbName,user,password);
ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=?");
ps.setString(1, EmployeeName);
ps.setString(2, Nationality);
ps.executeUpdate();
}
catch(Exception e)
{
out.print(e.getMessage());
e.printStackTrace();
}
%>
</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>
</body>
</html>
hey buddy's thanks for little conversations. I ill do some changes in my program and got the output., As the code is following as:
<%# page import="java.sql.*" %>
<%# page import="java.sql.DriverManager.*" %>
<%# page import="java.sql.Statement.*" %>
<%# page import="java.io.*" %>
<%
PreparedStatement ps;
Connection con=null;
ResultSet rs=null;
Statement st=null;
String driverName="com.mysql.jdbc.Driver";
String connectionUrl="jdbc:mysql://localhost:3306/";
String user="root";
String password="sarakrish";
String dbName="employee";
String emp_id = "";
String emp_name = "";
String nationality = "";
try{
Class.forName(driverName);
con=DriverManager.getConnection(connectionUrl+dbName,user,password);
if(request.getParameter("edit")!=null && request.getParameter("edit").toString().equals("yes")){
String EmployeeId = request.getParameter("emp_id");
String EmployeeName=request.getParameter("EmployeeName");
String Nationality=request.getParameter("Nationality");
ps=con.prepareStatement("update employees set EmployeeName=?,Nationlity=? where Employee_id=?");
ps.setString(1, EmployeeName);
ps.setString(2, Nationality);
ps.setString(3, EmployeeId);
ps.executeUpdate();
ps.close();
}
st = con.createStatement();
String sql="SELECT * FROM employees where employee_id='"+request.getParameter("emp_id")+"'";
rs=st.executeQuery(sql);
while (rs.next()) {
emp_id = rs.getString(2);
emp_name = rs.getString(1);
nationality = rs.getString(3);
break;
}
rs.close();
st.close();
con.close();
}catch(Exception e){
out.print(e.getMessage());
e.printStackTrace();
}
%>
<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>
<%=request.getParameter("edit")%>
<form action="editEmployee.jsp" name="UpdateForm" method="post">
<input type="hidden" name="edit" id="edit" value="yes">
<input type="hidden" name="emp_id" id="emp_id" value="<%=emp_id %>">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName" value="<%=emp_name %>"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality" value="<%=nationality %>"></td>
</tr>
</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>
</body>
</html>
Assuming this as string , since your Table details are unclear . Pass the arguments as String
ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=? where employee_id=? ");
ps.setString(1,'EmployeeName');
ps.setString(2,'Nationality');
ps.setString(3,1);
Also avoid using scriptlets , read through the link Aniket suggested.
Hope it helps !!
I'm trying to make a login page powered by asp and sql. After clicking submit, the form will not even trigger (i.e. the error message will not even display if the fields are blank)
Here is my code:
<!DOCTYPE html>
<html>
<title>
Query
</title>
<link href="http://hr-computing/public/AlexS/Tests/Site.css" rel="stylesheet">
<body>
<div id="main">
<%
username = ""
password = ""
ErrorMessage = ""
if request.form <> "" then
username = Request.Form("firstname")
password = Request.Form("password")
if username = "" or password = "" then
ErrorMessage = "You must specify a username and password."
else
set conn=Server.CreateObject("ADODB.Connection")
conn.Open ="{private}
set rs=Server.CreateObject("ADODB.recordset")
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select firstname, PASSWORD FROM teachers WHERE firstname = '" & firstname & "'", conn
if rs.EOF = false then
if rs.fields("Password") = password then
Response.Redirect("http://hr-computing/public/AlexS/Tests/default.asp")
end if
end if
ErrorMessage = "Login failed"
end if
end if
%>
<h1>Login</h1>
<form method="post" action="http://hr-computing/public/AlexS/Tests/login.asp">
<fieldset>
<legend>Log In to Your Account</legend>
<ol>
<li>
<label>Username:</label>
<input type="text" id="firstname" name="firstname" />
</li>
<li>
<label>Password:</label>
<input type="password" id="password" name="password" />
</li>
<li>
<p><input type="submit" value="Login" /></p>
</li>
</ol>
</fieldset>
</form>
</div>
</body>
</html>
Someone tell me what is wrong, please.
Where do you print out your error message? It doesn't look like you have that displayed anywhere. Classic ASP is kind of painful about this stuff - but somewhere you need a
<%= ErrorMessage %>
call to display the login error.
As you are using asp.net so for validating username and password for empty you can use required field validator. Be sure to set validation group to both the textbox as well as submit button.
i´m trying to update rows in mysql table, i´m using html form for data insertion. In html form attribute value i´m using an existing data from database.
Edit.jsp
<form action="Update.jsp">
<% Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select u.login,i.name,i.surname,i.age,i.tel,i.email,a.street,a.town,a.zip,ul.name,t.name from users u join info i on i.user_id=u.user_id join user_level ul on ul.ulevel=u.ulevel join teams t on t.team_id=u.team_id join adress a on a.info_id=i.info_id where u.login='" + session.getAttribute("uzivatel") + "'");
while (rs.next()) {
%>
<div class="well well-large">
<font size="2"><b>Welcome, </b></font><font color="RED"><i><%= session.getAttribute("uzivatel")%></i></font><br>
<b>Name:</b> <input type="text" name="name" value="<%= rs.getString(2)%>"><input type="text" name="surname" value="<%= rs.getString(3)%>"> <br>
<b>Age:</b> <input type="text" name="age" value="<%= rs.getString(4)%>"><br>
<b>Telephone:</b> <input type="text" name="tel" value="0<%= rs.getString(5)%>"><br>
<b>E-mail:</b> <input type="text" name="email" value="<%= rs.getString(6)%>"><br>
<b>Adress:</b> <input type="text" name="street" value="<%= rs.getString(7)%>"><input type="text" name="town" value="<%= rs.getString(8)%>"><input type="text" name="zip" value="<%= rs.getString(9)%>"><br>
<b>User level:</b> <%= rs.getString(10)%><br>
<b>Team:</b> <%= rs.getString(11)%><br>
<input type="submit" name="Submit" value="Update" />
</div>
</form>
Update.jsp
<%
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String age = request.getParameter("age");
String telephone = request.getParameter("tel");
String email = request.getParameter("email");
String street = request.getParameter("street");
String town = request.getParameter("town");
String zip = request.getParameter("zip");
try {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st1 = null;
st1 = conn.createStatement();
System.out.println(session.getAttribute("uzivatel"));
st1.executeUpdate("UPDATE users JOIN info ON users.user_id = info.user_id"
+ " JOIN adress ON info.info_id = adress.info_id"
+ "SET info.name = '"+name+"',info.surname = '"+surname+"',"
+ "info.age = '"+age+"',info.tel = '"+telephone+"',info.email = '"+email+"',"
+ "adress.street = '"+street+"',adress.town = '"+town+"',adress.zip = '"+zip+"',"
+ "WHERE users.login ='" + session.getAttribute("uzivatel") + "' ");
response.sendRedirect("AdministrationControlPanel.jsp");
} catch (Exception e) {
System.out.println(e.getMessage());
}
%>
When I pressed the Submit button, it redirected me to Update.jsp and nothing was changed.
There is problem in your query that is why you are not getting any result. give space at properly when you are dividing the string in various lines.
system.out.println() will show message on the console not on the page
st1.executeUpdate("UPDATE users JOIN info ON users.user_id = info.user_id"
+ " JOIN adress ON info.info_id = adress.info_id "
+ "SET info.name = '"+name+"',info.surname = '"+surname+"', "
+ "info.age = '"+age+"', info.tel = '"+telephone+"', info.email = '"+email+"',"
+ "adress.street = '"+street+"',adress.town = '"+town+"',adress.zip = '"+zip+"', "
+ "WHERE users.login ='" + session.getAttribute("uzivatel") + "' ");
Better you use jQuery rather than calling page update.jsp. When you use jQuery that time you can send data to update.jsp page with parameter and take response from that page and print it in you page even you can take all table data from the jQuery
It is very simple Hope this will help you.. for sample example have look of following urls
demo
http://www.roseindia.net/tutorial/jquery/jqDatabaseConnect.html
http://api.jquery.com/jQuery.post/