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









Related Tutorials/Questions & Answers:
How to call servlet in JSP?
JSP1
Advertisements
JSP2
ModuleNotFoundError: No module named 'jspp'
ModuleNotFoundError: No module named 'JSPy'
How to make a servlet to automatically call another servlet at some time instant.
how to call jsp variable through servlet
servlet n jsps - Java Beginners
How to call jasper from jsp or servlet - JSP-Servlet
how to call a java class in jsp - JSP-Servlet
how to call a java class in jsp - JSP-Servlet
how to create own tld and use it in jspx file
how to create own tld and use it in jspx file
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 ?
Call a servlet on click of hyperlink
how to write jsps and databasetables,umldiagrams in fuelmanagement system project
Call servlet from javascript - JSP-Servlet
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 call ireports in jsp
Diff ways to call a EJB from Servlet, JSP - Java Interview Questions
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
call dll from java - JSP-Servlet
Call dll from java - JSP-Servlet
application of remote procedure call - JSP-Servlet
remote procedure call using java - JSP-Servlet
Protect JSPs from direct access
how to call jsp from flex
how to call jsp from flex
versions of servlets and JSPs?
error got minus one from read call - JSP-Servlet
JSPs : Declarations
How do you call a constructor for a parent class?
How to call java method on Button click in jsp?
how to call javascript function from flex
how to call ejb with in another ejb - EJB
How to use Servlet and Ajax?
How to call print dialog box through JMenuItem?
how to call the print dialog box through JMenuItem
how to call from pc to telephone in java - MobileApplications
How to make phone call from web application
How to call Array Length for loop in PHP
how to call a frame having threading concept
How to call the run method from another class?

Ads