从HTML表单调用servlet,但从不调用servlet [英] Calling servlet from HTML form, but servlet is never invoked

查看:127
本文介绍了从HTML表单调用servlet,但从不调用servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam从html表单调用servlet,servlet获取表单数据,并将该表单数据插入到数据库中。但是,当我单击提交按钮时,错误页面即将到来。请在我的servlet代码中帮助whats错误。



我的servlet代码:

  import javax.servlet.http.HttpServlet; 

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



$ b 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;
连接连接=空;
res.setContentType(text / html);
PrintWriter out = res.getWriter();
字符串用户名= req.getParameter(用户名);
String password = req.getParameter(password);
尝试{
Class.forName(com.mysql.jdbc.Driver);
connection = DriverManager.getConnection(connectionURL,root,root);
String sql =插入注册值(?,?);
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(无法加载数据库驱动程序:+ 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){
out.println(忽略);
}
}
}
}

我的html代码:




注册





 < form action =servlet / Loginservletmethod =post> 

< font size ='5'>建立您的帐户:< / font>< br />< br>

< label for =usernameaccesskey =ustyle =padding-left:3px;>用户名:< / label>

< input type =textstyle =background-color:#ffffff; margin-left:14px; padding-top:7px; border-width:0px; margin-top:6px;填充右:85px; id =usernamename =usernametabindex =1>< br />< br>

< label for =passwordaccesskey =pstyle =padding-left:4px;>密码:< / label>

< input type =passwordstyle =background-color:#ffffff; margin-left:14px; padding-top:7px; border-width:0px; padding-right:85px; id =passwordname =paswordtabindex =2>< br />< br>


< input type =submitvalue =Submitstyle =margin-left:164px;/>

< input type =resetvalue =Resetstyle =margin-left:17px;/>

< / form>





web.xml文件:

 <?xml version =1.0encoding = 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的/网络app_2_5.xsd>

< servlet>
< servlet-name>登录< / servlet-name>
< servlet-class> Loginservlet< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name>登录< / servlet-name>
< url-pattern> / login< / url-pattern>
< / servlet-mapping>



请帮忙

解决方案

当您查看您的servlet类时,没有定义 package 需要。并在< servlet-class /> 标记中将该类与包(意思是完全限定名)映射。



另一件事是你将动作设置为url servlet / LogininServlet ,但在< url-pattern /> 标签,这是错误的。您可以简单地将表单操作设置为 login


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

解决方案

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

这篇关于从HTML表单调用servlet,但从不调用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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