Let’s consider a problem task. Let we have a signup form taking four attributes like Name, Contact, Email, Address.
When the user will give the details and click on signup button then SignupServlet.java will execute. The purpose of servlet is to process form data and contain JDBC code , as a result data will go the database(signup table).
Here we are using MySQL as database.
First we will create a table let named as signup table in MySQL environment.
create table signupdb.signup
(
Name varchar(200) not null,
Contact varchar(200) not null,
Email varchar(200) primary key,
Address varchar(200) not null
)
Then go to Eclipse and develop the required files.
<html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center><h2>SignUp Form</h2> <form action="SignupServlet"> <table> <tr> <td>Name</td> <td><input type="text"name="name"></td> </tr> <tr> <td>Contact</td> <td><input type="text"name="contact"></td> </tr> <tr> <td>Email</td> <td><input type="email"name="email"></td> </tr> <tr> <td>Address</td> <td><input type="text"name="address"></td> </tr> <tr> <td><input type="submit"value="signup"></td> </tr> </table> </form> </center> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>MyServletProjects</display-name> <servlet> <servlet-name>abc</servlet-name> <servlet-class>com.silan.SignupServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>abc</servlet-name> <url-pattern>/SignupServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>signup.html</welcome-file> </welcome-file-list> </web-app>
Output:
Then you go to MySQL environment and execute Select * from signupdb.signup You will get :
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc