aspose file tools*
The moose likes JSP and the fly likes JSP problem in code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP problem in code" Watch "JSP problem in code" New topic
Author

JSP problem in code

salman haider
Greenhorn

Joined: Jan 05, 2010
Posts: 2
Hello Everyone,
Happy New Year.

I am a beginner in jsp . I have written code for 3 jsp pages and I have used mysql database. My aim was to give login page on first page, then to give a form on 2nd page for submitting the details of a person. My first page is working, as I have added the login of only one person, but my problem is, my second page is not working. I would be thankful if someone can correct my code.I am pasting the code below. I want that when the user click on submit button on second page the values filled by him should be stored in the database.

PAGE 1 code:

<html>
<body>
<head><title> My Website </title></head>

<body>


<h1> Welcome to my homepage </h1>
<table border = "1" >
<tr >
<td>
<form method = "post" action = "regform.jsp">
<pre>

username: <input type = "text" name = "user" >
password: <input type = "password" name = "pass" >

<input type = "submit" value = "login">
</pre>
</form>
</td>
</tr>

</body>
</html>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2nd Page code:

<%@ page import = "java.sql.*" %>
<%@ page import = "java.net.*" %>

<html>
<body>



<%


String driver = "com.mysql.jdbc.Driver";
String username = "root";
String password = "germany";
String url = "jdbc:mysql://localhost:3306/example?" + "user=" + username + "&password=" + password;
String s = request.getParameter("fname");
String a = request.getParameter("lname");
//String g = request.getParameter("gender");
String b = request.getParameter("country");
String c = request.getParameter("address");
String e = request.getParameter("zip");
String g = request.getParameter("gender");





try
{

Class.forName(driver);

Connection conn = DriverManager.getConnection(url,username,password);
Statement stmt = conn.createStatement();

//String query = "insert into registration1 values('"+s+"', '"+g+"')";

String query = "insert into registration2 values('"+s+"', '"+a+"','"+b+"', '"+c+"','"+e+"', '"+g+"')";

int i = stmt.executeUpdate(query);
if(i > 0)
out.println("Data entered succesfully");

else

out.println("some issue");
conn.close();


}



catch(ClassNotFoundException ex)
{
System.out.println("exception occured:" + ex);

}

catch (SQLException e) {

e.printStackTrace();
}

%>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PAGE 3 code:

<html>
<head><title> Registration Page </title> </head>
<body>

<h1 align = "center"> Registration </h1>

<form method = "post" action = "registration.jsp">
<pre>
First Name : <input type = "text" name ="fname">
Last Name : <input type = "text" name ="lname">
Country : <input type = "text" name ="country">
Address : <input type = "text" name ="address">
ZIP : <input type = "text" name ="zip">

Gender:
<input type = "radio" name ="gen" value = "male"> Male
<input type = "radio" name ="gen" value = "female"> Female
<input type = "submit" value = "register">
</pre>
</form>

</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MySql database query code:

mysql> create table registration2
-> (
-> fname varchar(20),
-> lname varchar(20),
-> gender varchar(20),
-> country varchar(20),
-> address varchar(30),
-> zip varchar(20)
-> );
Query OK, 0 rows affected (0.16 sec)


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Firstly, your second "page" shouldn't be a page at all. Modern JSPs should contain no Java code. That code should be in a servlet.

Secondly, please read this. Just telling us that it doesn't work without any further explanation, does not help us to help you.
salman haider
Greenhorn

Joined: Jan 05, 2010
Posts: 2
Hello Everyone,

I am new on this website, can anybody tell me how to put code in tags. I would be very thankful.

Regards,
Salman.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41510
    
  53
The first link Bear posted explains how to use code tags; that's why he posted it


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP problem in code