• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

JSP problem in code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)


 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first link Bear posted explains how to use code tags; that's why he posted it
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic