通过servlet错误注册用户 [英] Registration of user through servlet error

查看:87
本文介绍了通过servlet错误注册用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个JSP页面,其中包含一个创建用户表单,一个servlet和我的web.xml文件.当我点击提交按钮时,什么也没有发生.我没有收到任何错误,什么也没发生.谁能看到我做错了什么?

最好的问候 疯子

JSP:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
    <form action="Registration" method="POST">
        Firstname:  <input type="text" name="firstname" value="">
        Lastname:   <input type="password" name="lastname" value="">
                    <input type="button" value="submit"></input>
    </form>

</body>
</html>

Servlet:

package myservlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Registration extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/logindatabase";
        Connection connection=null;
        ResultSet rs;
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        String fname = req.getParameter("firstname");
        String sname = req.getParameter("lastname");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            String sql = "INSERT INTO login VALUES (?,?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, fname);
            pst.setString(2, sname);

            int numRowsChanged = pst.executeUpdate();
            out.println(" Welcome : ");
            out.println(" '"+fname+"'");
            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);
            }
        }
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <description>
        User Registration and Login Example.
    </description>
    <display-name>User Registration and Login Example</display-name>
    <servlet>
        <servlet-name>Registration</servlet-name>
        <servlet-class>myservlets.Registration</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Registration</servlet-name>
        <url-pattern>/Registration</url-pattern>
    </servlet-mapping>
</web-app>

解决方案

他(@Aniket Kulkarni)说的没错.但是您几乎正确地走了正确的路.这些是您提交表格的几种方法.只需看看我的模型如下:

<input type="button">

按钮INPUT标记提供了许多与图像INPUT标记相同的选项,但看起来更像是标准提交类型.它需要JavaScript才能激活.

<button></button>

BUTTON标签比INPUT标签具有更多用途.此标记需要Javacript才能激活.

<command type="command">

COMMAND元素是HTML5中的新增功能,它提供了一种激活具有相关操作的脚本和表单的方法.它使用JavaScript激活.

<&&&&&&&&&&&&&&&&&&&&&&&& amp;& amp; &&&&&&&&&&&&&&&&&&&& ;&&&

INPUT元素

INPUT元素是提交表单的最常用方法,您要做的就是选择一种类型(按钮,图像或提交),并在必要时添加一些脚本以提交给表单操作.

元素可以这样写.但是,如果这样做,您将在不同的浏览器中得到不同的结果.大多数浏览器的按钮均显示为提交",而Firefox的按钮则显示为提交查询".要更改按钮的内容,您应该添加一个value属性:

<input type="submit" value="Submit Form">

元素的写法是这样的,但是如果您忽略所有其他属性,那么将在浏览器中显示的所有内容都是一个空的灰色按钮.要将文本添加到按钮,请使用value属性.但是,除非您使用JavaScript,否则此按钮不会提交表单.

<input type="button" value="Submit Form" onclick="submit();">

类似于按钮类型,它需要脚本来提交表单.除了文本值以外,您还需要添加图像源URL.

<input type="image" onclick="submit();" src="submit.gif">

<&&&&&&&&&&&&&&&&&&&&&&&& amp;& amp; &&&&&&&&&&&&&&&&&&&&&&&&& ;&&&

按钮元素

BUTTON元素同时需要一个开始标记和一个结束标记,当您使用它时,您封装在标记内的所有内容都将包含在一个按钮中.然后使用脚本激活按钮.

<button onclick="submit();">Submit Form</button>

您可以在按钮中包含图像,也可以将图像和文本组合在一起以创建更有趣的按钮.

<button onclick="submit();"><img src="submit.gif" />Submit Form</button>

<&&&&&&&&&&&&&&&&&&&&&&&& amp;& amp; &&&&&&&&&&&&&&&&&&&&&&&&& ;&&&

COMMAND元素

COMMAND元素是HTML5的新功能.它不需要使用FORM,但可以用作表单的提交按钮.除非您确实需要表单,否则此元素使您无需表单即可创建更多的交互式页面.如果要命令说些什么,可以将信息写在label属性中.

<command type="command" onclick="submit();" label="Submit Form">

如果要用图像表示命令,请使用icon属性.

<command type="command" onclick="submit();" icon="submit.gif">

<&&&&&&&&&&&&&&&&&&&&&&&& amp;& amp; &&&&&&&&&&&&&&&&&&&&&&&&& ;&&&

我想我已经清除了您对所有提交类型的怀疑.但是,现在您只需遵循此输入元素并运行程序即可.

<input type="submit" value="Submit Form" or value="Submit">

I made a JSP page with a create user form, a servlet and my web.xml file. When I hit the submit button nothing happens. I don't get an error or something, just nothing happens. Can anyone see what it is I do wrong?

Best Regards Mads

JSP:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
    <form action="Registration" method="POST">
        Firstname:  <input type="text" name="firstname" value="">
        Lastname:   <input type="password" name="lastname" value="">
                    <input type="button" value="submit"></input>
    </form>

</body>
</html>

Servlet:

package myservlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Registration extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/logindatabase";
        Connection connection=null;
        ResultSet rs;
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        String fname = req.getParameter("firstname");
        String sname = req.getParameter("lastname");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            String sql = "INSERT INTO login VALUES (?,?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, fname);
            pst.setString(2, sname);

            int numRowsChanged = pst.executeUpdate();
            out.println(" Welcome : ");
            out.println(" '"+fname+"'");
            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);
            }
        }
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <description>
        User Registration and Login Example.
    </description>
    <display-name>User Registration and Login Example</display-name>
    <servlet>
        <servlet-name>Registration</servlet-name>
        <servlet-class>myservlets.Registration</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Registration</servlet-name>
        <url-pattern>/Registration</url-pattern>
    </servlet-mapping>
</web-app>

解决方案

He's (@Aniket Kulkarni) telling right. But your going right way for almost you got it. These are several ways you can submit your forms. Just look at my model are following as:

<input type="button">

The button INPUT tag gives a lot of the same options as the image INPUT tag, but looks more like the standard submit type. It requires JavaScript to activate.

<button></button>

The BUTTON tag is a more versatile type of button than the INPUT tag. This tag requires Javacript to activate.

<command type="command">

The COMMAND element is new in HTML5, and it provides a way to activate scripts and forms with associated actions. It is activated with JavaScript.

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

The INPUT Element

The INPUT element is the most common way to submit a form, all you do is choose a type (button, image, or submit) and if necessary add some scripting to submit to the form action.

The element can be written just like that. But if you do, you will have different results in different browsers. Most browsers make a button that says "Submit," but Firefox makes a button that says "Submit Query." To change what the button says, you should add a value attribute:

<input type="submit" value="Submit Form">

The element is written like that, but if you leave off all other attributes, all that will display in browsers is an empty gray button. To add text to the button, use the value attribute. But this button won't submit the form unless you use JavaScript.

<input type="button" value="Submit Form" onclick="submit();">

The is similar to the button type, which needs a script to submit the form. Except that instead of a text value, you need to add an image source URL.

<input type="image" onclick="submit();" src="submit.gif">

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

The BUTTON Element

The BUTTON element requires both an opening tag and a closing tag When you use it, any content you enclose inside the tag will be enclosed in a button. Then you activate the button with a script.

<button onclick="submit();">Submit Form</button>

You can include images in your button or combine images and text to create a more interesting button.

<button onclick="submit();"><img src="submit.gif" />Submit Form</button>

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

The COMMAND Element

The COMMAND element is new with HTML5. It does not require a FORM to be used, but it can act as a submit button for a form. This element allows you to create more interactive pages without requiring forms unless you really need forms. If you want the command to say something, you write the information in a label attribute.

<command type="command" onclick="submit();" label="Submit Form">

If you want your command to be represented by an image, you use the icon attribute.

<command type="command" onclick="submit();" icon="submit.gif">

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

I think I've clear your doubt for all submit types. But, now you just follow this input element and run your program.

<input type="submit" value="Submit Form" or value="Submit">

这篇关于通过servlet错误注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆