How to call servlet in JSP?

How to call servlet in JSP?

How to call servlet in JSP?

View Answers

January 20, 2011 at 10:44 AM

Hi Friend,

Try the following code:

1)form.jsp:

<html>
<form action="../InsertServlet" method="post">
<table align=center border=0 cellspacing=0 cellpadding=10 width=100 height=0 bgcolor=#faebd7>
<tr>
<td>Name</td>
<td><input type=text name="name" size=20></td>
</tr>
<tr>
<td>Address</td>
<td><input type=text name="address" size=30></td>
</tr>
<tr>
<td>Tel no</td>
<td><input type=text name="no" size=20></td>
</tr>
</table>
<br>
<br>
<center>
<input type=submit name=submit value=submit>
</form>
</html>

2)InsertServlet.java:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertServlet extends HttpServlet { 
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String name=req.getParameter("name");
        String address=req.getParameter("address");
        int no=Integer.parseInt(req.getParameter("no"));
        try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        Statement st=con.createStatement();
        int i=st.executeUpdate("insert into data(name,address,telno) values('"+name+"','"+address+"',"+no+")");
        out.println("Data is inserted successfully");
        con.close();
        }
        catch(Exception e){
        System.out.println(e);
        }
    }
}

For more information, visit the following links:

JSP Tutorials

Servlet Tutorials

Thanks

Ads









Related Tutorials/Questions & Answers:
How to call servlet in JSP?
JSP1
Advertisements
JSP2
How to make a servlet to automatically call another servlet at some time instant.
How can I call a Servlet that is declare on web.xml with spring framework
how to call servlet from html page at anchor tag ?
how to create own tld and use it in jspx file
how to create own tld and use it in jspx file
Call a servlet on click of hyperlink
ModuleNotFoundError: No module named 'jspp'
ModuleNotFoundError: No module named 'JSPy'
how to call our servlet once the credentials and url provided in the Salesforce login page.
how to call our servlet once the credentials and url provided in the Salesforce login page.
how to write jsps and databasetables,umldiagrams in fuelmanagement system project
how call ireports in jsp
how to call static method
how to call static method
how to call static method
how to call static method
how to call static method
how to call static method
How to use Servlet and Ajax?
versions of servlets and JSPs?
How to call java method on Button click in jsp?
how to call javascript function from flex
How to call print dialog box through JMenuItem?
how to call the print dialog box through JMenuItem
How to make phone call from web application
How to find a servlet object by name?
How to call Array Length for loop in PHP
how to call a frame having threading concept

Ads