Calling servlet from HTML form, but servlet is never invoked
Solution 1
check web.xml
file of your project, you have to registrate your servlet there.check this also
use <form action="/login" method="post" >
in html
and
in web.xml
<servlet-class>your.class.package.Loginservlet</servlet-class>
</servlet>
Solution 2
As you look into your servlet class, there is no package
defined, which is required. And map that class with package(mean fully qualified name) in <servlet-class/>
tag.
Another thing is you are setting action to url servlet/LogininServlet
, but given different url in <url-pattern/>
tag, which is wrong. you can simply set the form action to login
krishna bhargavi
Updated on June 13, 2022Comments
-
krishna bhargavi almost 2 years
Iam calling servlet from html form,servlet takes the form data and it will insert that form data into database.But when i click the submit button error page is coming.please help whats wrong in my servlet code.
my servlet code:
import javax.servlet.http.HttpServlet; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Loginservlet extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { System.out.println("login servlet"); String connectionURL = "jdbc:mysql://localhost:3306/mysql"; Connection connection=null; res.setContentType("text/html"); PrintWriter out = res.getWriter(); String username= req.getParameter("username"); String password = req.getParameter("password"); try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", "root"); String sql = "insert into signup values (?,?)"; PreparedStatement pst = connection.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); int numRowsChanged = pst.executeUpdate(); out.println(" Data has been submitted "); 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); } } } }
my html code:
Sign Up
<form action="servlet/Loginservlet" method="post" > <font size='5'>Create your Account:</font><br/><br> <label for="username" accesskey="u" style="padding-left:3px;">User Name: </label> <input type="text" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;margin-top:6px;padding-right:85px;" id="username" name="username" tabindex="1"><br/><br> <label for="password" accesskey="p" style="padding-left:4px;">Password: </label> <input type="password" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;padding-right:85px;" id="password" name="pasword" tabindex="2"><br/><br> <input type="submit" value="Submit" style="margin-left:164px;"/> <input type="reset" value="Reset" style="margin-left:17px;"/> </form>
web.xml file:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>login</servlet-name> <servlet-class>Loginservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
please help