浏览器在调用servlet时不显示任何内容

时间:2017-10-08 07:09:03

标签: java mysql servlets jdbc web-applications

我尝试了一段时间,但每次当我尝试在浏览器上运行时,它都没有显示任何内容,甚至没有显示错误。

“index.html”文件

<html>
<head>
    <title>Register form</title>
</head>
<body>
    <form method="post" action="register">
    Name:<input type="text" name="name" /><br/>
    Email ID:<input type="text" name="email" /><br/>
    Password:<input type="text" name="pass" /><br/>
    <input type="submit" value="register" />
    </form>
</body>

“web.xml”文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app  version="3.0" 
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_3_0.xsd" >

    <servlet>
        <servlet-name>register</servlet-name>
        <servlet-class>Register</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>register</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

“Register.java”文件

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

public class Register extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
        try{

        //loading drivers for mysql
        Class.forName("com.mysql.jdbc.Driver");

    //creating connection with the database 
          Connection  con=DriverManager.getConnection
                     ("jdbc:mysql:/ /localhost:3306/db","myuser","1234");

        PreparedStatement ps=con.prepareStatement
                  ("insert into Student values(?,?,?)");

        ps.setString(1, name);
        ps.setString(2, email);
        ps.setString(3, pass);
        int i=ps.executeUpdate();

          if(i>0)
          {
            out.println("You are sucessfully registered");
          }

        }
        catch(Exception se)
        {
            se.printStackTrace();
        }

      }
  }

This image is after i entered the details, and there is no data stored in my database

This is the error showing in cmd

我正在使用JRE中没有“ext”文件的JDK 9,所以我通过命令行包含了JDBC驱动程序文件。

我在mysql和表名'student'中创建了数据库'db'。

我不知道问题出在哪里。请帮我!!

2 个答案:

答案 0 :(得分:-1)

您的表单方法发布

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

在你的servlet中你只有一个doGet。

将表单方法更改为获取

<form method="get" action="register">

Servlet doGet:

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
        try{

        //loading drivers for mysql
        Class.forName("com.mysql.jdbc.Driver");

    //creating connection with the database 
          Connection  con=DriverManager.getConnection
                     ("jdbc:mysql:/ /localhost:3306/db","myuser","1234");

        PreparedStatement ps=con.prepareStatement
                  ("insert into Student (name,email,pass) values(?,?,?);");

        ps.setString(1, name);
        ps.setString(2, email);
        ps.setString(3, pass);
        ps.executeUpdate();



        }catch(Exception se){
            se.printStackTrace();
        }
    //redirect user back to index
    RequestDispatcher rd=request.getRequestDispatcher("index.html");        
    rd.forward(request,response);  

      }
  }

答案 1 :(得分:-1)

您不会关闭Connection和PreparedStatement。 最好使用try-with-resources。连接URL中有一个空格。 另一个问题已经提到了。

   try (Connection con=DriverManager.getConnection
                 ("jdbc:mysql://localhost:3306/db","myuser","1234");
            PreparedStatement ps=con.prepareStatement
                 ("insert into Student (name,email,pass) values(?,?,?)")) {

        ps.setString(1, name);
        ps.setString(2, email);
        ps.setString(3, pass);
        ps.executeUpdate();
    } catch(Exception se) {
        se.printStackTrace();
    }