Sie sind auf Seite 1von 4

<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:

Das könnte Ihnen auch gefallen