Anda di halaman 1dari 10

View Latest Questions and Answers|Ask Questions?

Home Java Frameworks Database Technology Web Development Build/Test Tools Servers PHP

Home Quickguide Tomcat Develop user registration form

Google Advance Search RoseIndia.Net Search

Develop user registration form Posted on: July 8, 2008 at 12:00 AM In this example we are going to work with a user registration page and saving the data into the database.

User Registration Form in JSP

In this example we are going to work with a user registration page and saving the data into the database. To store the data in the database table, create a table in which the values will be inserted. Now make one user registration jsp page. In this example we will create a simple registration form which will insert the entered data into the database successfully with the help of a servlet. When submit button is clicked the data will be inserted into the database. Firstly the registration form data will go to the controller servlet and search for the variables submitted in the form using the method getParameter() method at the request object. Subsequently it passes a query statement to the database to insert the data retrieved from the registration form. To stored the data into the database it uses setString() method and to retrieve the data from the database it uses the getString() method of PreparedStatement object. The code of the program is given below: <html> <body bgcolor="#999966">

<form action="/userregister/Registration " method=post> <table cellpadding=4 cellspacing=2 border=0> <th bgcolor="#999966" colspan=2> <font size=5>REGISTRATION</font> <br><font size=1><sup></sup></font><hr> </th><tr bgcolor="#999966"><td valign=top> <b>First Name<sup>*</sup></b> <br><input type="text" name="firstname" value="" size=15 maxlength=20> </td><td valign=top><b>Last Name<sup>*</sup></b> <br><input type="text" name="surname" value="" size=15 maxlength=20></td> </tr><tr bgcolor="#999966"> <td valign=top><b>E-Mail<sup>*</sup></b> <br><input type="text" name="email" value="" size=25 maxlength=125><br></td><td valign=top> <b>Zip Code<sup>*</sup></b> <br> <input type="text" name="zipcode" value="" size=5 maxlength=6></td> </tr><tr bgcolor="#999966"><td valign=top colspan=2> <b>User Name<sup>*</sup></b><br> <input type="text" name="userId" size=10 value="" maxlength=10> </td></tr><tr bgcolor="#999966">

<td valign=top><b>Password<sup>*</sup></b> <br><input type="password" name="address1" size=10 value="" maxlength=10></td><td valign=top> <b>Confirm Password<sup>*</sup></b> <br><input type="password" name="address2" size=10 value="" maxlength=10></td><br> </tr><td valign=top> <b>Town:<sup>*</sup></b> <br><input type="text" name="town" size=10 value="" maxlength=10></td> <br></tr><td valign=top> <b>City:<sup>*</sup></b> <br><input type="text" name="country" size=10 value="" maxlength=10></td><br> </tr><tr bgcolor="#663300"> <td align=center colspan=2><hr> <input type="submit" value="Submit"></td></tr></table></center> </form> </body> </html> Registration .java package myservlets; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Registration extends HttpServlet{ public void init(ServletConfig config) throws ServletException{ super.init(config); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ String connectionURL = "jdbc:mysql://localhost/jsp"; Connection connection=null; ResultSet rs; res.setContentType("text/html"); PrintWriter out = res.getWriter(); String uId = req.getParameter("userId"); String fname = req.getParameter("firstname"); String sname = req.getParameter("surname"); String address1 = req.getParameter("address1"); String address2 = req.getParameter("address2");

String town = req.getParameter("town"); String county = req.getParameter("country"); String zipcode = req.getParameter("zipcode"); try { Class.forName("org.gjt.mm.mysql.Driver"); connection = DriverManager.getConnection(connectionURL, "root", "root"); String sql = "insert into userprofile values (?,?,?,?,?,?,?,?)"; PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, uId); pst.setString(2, fname); pst.setString(3, sname); pst.setString(4, address1); pst.setString(5, address2); pst.setString(6, town); pst.setString(7, county); pst.setString(8, zipcode); int numRowsChanged = pst.executeUpdate(); out.println(" Welcome : "); out.println(" '"+fname+"'"); pst.close(); } catch(ClassNotFoundException e){ out.println("Couldn't load database driver: " + e.getMessage()); } catch(SQLException e){ out.println("SQLException caught: " + e.getMessage()); } catch (Exception e){ out.println(e); } finally { try { if (connection != null) connection.close(); } catch (SQLException ignored){ out.println(ignored); } } } } web.xml file for this program: <?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5"> <description> User Registration and Login Example. </description> <display-name>User Registration and Login Example</display-name> <servlet> <servlet-name>Registration</servlet-name> <servlet-class>myservlets.Registration</servlet-class> </servlet> <servlet-mapping> <servlet-name>Registration</servlet-name> <url-pattern>/Registration</url-pattern> </servlet-mapping> </web-app> The output of the program is given below:

The output of the input data:

Download Source Code Related Tags for Develop user registration form: c, database, jsp, table, data, insert, io, make, user, gis, page, vi, value, int, this, registration, create, tab, js, example, with, work, values, to, base, exam, store, avi, e, il, it, rte, use, in, no, saving, as, m, nt, tr, j, es, ase, age, into, tor, xa, which, xamp, s, sp, at, ratio, k, is, ll, mpl, go, gist, ea, and, ar, str, rt, sa, vin, va, s, s, th, av, st, ab, abl, ati, alu, ple, pl, nd, on, o Previous Index Next

Ask Questions?

Discuss: Develop user registration form View All Comments

Post your Comment

Your Name (*) : Your Email : Subject (*):

Your Comment (*):

Ask Questions? If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for. Ask your questions, our development team will try to give answers to your questions. Tutorial Topics

Hibernate Framework( 1057 ) Struts Framework( 836 ) Spring Framework( 567 ) XML( 196 ) Ajax( 528 ) JavaScript( 109 ) Java( 1806 ) Web Services( 71 ) Database( 145 ) Technology( 90 ) Web Development( 503 ) PHP( 406 )

Services Software Solutions


JSF Development Outsourcing ERP M-Commerce Flex Development Website Development

Web Designing Web Redesigning Web Development Logo Design Web Design Packages Domain Registration Web Promotion Services

SEO Services Search Eng. Optimization Search Eng. Submission SEO Tips SEO Portfolio Web Promotion Plans Content Development

Content Development Article Writing Blog Writing News Writing SEO Copywriting Technical Documentation Article Marketing Web Hosting Services

ASP.NET Hosting Unix Hosting E-Commerce Hosting Windows Hosting Hosting Plan

E-Commerce Solutions

CRM

Home Privacy Policy All Rights are Reserved for Rose India