0

Possible Duplicate:
Calling servlet from HTML form, but servlet is never invoked

I'm 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 html code:

<html>
    <head>
        <title> Sign Up </title>
    </head>
    <body>
        <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>
    </body>
</html>

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 web.xml:

<?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>  
</web-app>
Community
  • 1
  • 1
krishna bhargavi
  • 961
  • 4
  • 15
  • 24
  • 1
    If your going to multi-post questions, please at least learn how to format code and HTML. Editing every one of your posts would quickly become tiresome! – Andrew Thompson Dec 14 '11 at 12:39

3 Answers3

1

I think, mistake is here: <form action="servlet/Loginservlet"

Try this <form action="/<your-app-name>/login"

korifey
  • 3,379
  • 17
  • 17
0

since in form action you have given servlet/Loginservlet . you should map same in web.xml like

<url-pattern>/servlet/Loginservlet</url-pattern>

or change in the form like

action='login'

you can do any one of above.

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
0

try this

<form action="login"  method="post" >

you servlet url is

<url-pattern>/login</url-pattern>

When you run your web application for local host then the url like

localhost:8080/test

then the test is the name of your application name which contain your web application into the web dir. Now suppose you create the index file then it will run index file from this only and another page url like

localhost:8080/test/page1.html

now from page1.html you start your login page then the link looks like for servlet is

localhost:8080/test/login

as define the url pattern for specific servlet that url you have to set for calling the specific servlet like above. this url will hide the actual servlet for the client you can set anything in url pattern

Pratik
  • 30,639
  • 18
  • 84
  • 159