在eclipse中使用servlets/mysql创建登录/注册页时出现问题

yhived7q  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(392)

所以这是我的问题。我正在做一个学校的项目,我基本上撞到了墙。我根本无法让我的注册服务器工作。我可以让它运行,并正确显示它背后构建的registration.jsp,但没有任何信息输入到数据库中。事实上,我甚至不知道这是否正是我失败的地方。以下是注册功能的代码:
注册.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<style>
div.container {
    //width: 100%;
   // border: 1px solid gray;
  //  padding:5px;

}

table, th, td {
   //border: 1px solid black;
}
.textright{float:right;padding:10px}

</style>

<title>Registration</title>
<script> 
function validate()
{ 
var firstname = document.form.firstname.value;
var lastname = document.form.lastname.value;
var username = document.form.username.value; 
var password = document.form.password.value;
var address = document.form.address.value;
var contact = document.form.contact.value;
if (firstname==null || firstname=="")
{ 
alert("First Name can't be blank"); 
return false; 
}
else if (lastname==null || lastname=="")
{ 
alert("Last Name can't be blank"); 
return false; 
}
else if (username==null || username=="")
{ 
alert("Username can't be blank"); 
return false; 
}
else if (password==null || password=="")
{ 
alert("Password can't be blank"); 
return false; 
}
else if(password.length<6)
{ 
alert("Password must be at least 6 characters long."); 
return false; 
}
else if (address==null || address=="")
{ 
alert("Address can't be blank"); 
return false; 
}
else if (contact==null || contact=="")
{ 
alert("Contact can't be blank"); 
return false; 
}
} 
</script> 
</head>

<body bgcolor="#D3D3D3">
<form name="form" action="RegistrationServlet" method="post" 
onsubmit="return 
validate()">
<td style="float:left"><image 
src="file:///G:/College/Winter%202018/CSC%20363/Computer%20logo.png" 
width="100px" height="100px"></image></td>
<p style="float:right"><b> ${members[3]} <br> CSC 363<br>Final Project</b> 
</p>

        <td style="clear:float"></td>
        <p style="align-text"left">Team</p>
        <p style="float:left">${members[0]} <br> ${members[1]} <br> 
        ${members[2]} </p>
        <td style="close:float"></td>

<table style="float:right">
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastName" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Contact</td>
<td><input type="text" name="contact" /></td>
</tr>

<tr>
<td><%=(request.getAttribute("errMessage") == null) ? ""
: request.getAttribute("errMessage")%></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></input>
</tr>
</table>
</form>
</body>
</html>

注册服务

package com.mvc.RegistrationServlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvc.Bean.RegistrationBean;
import com.mvc.Dao.RegistrationDao;
public class RegistrationServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public RegistrationServlet() {
}

protected void doGet(HttpServletRequest request, HttpServletResponse 
response) 
throws ServletException, IOException {
// TODO Auto-generated method stub

String[] members = new String[4];
 members[0] = "Jamie W.";
 members[1] = "Evan D.";
 members[2] = "Tim D.";
 members[3] = "Hardware Plus";

request.setAttribute("members", members);
request.getRequestDispatcher("/Registration.jsp").forward(request, 
response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse 
response) 
throws ServletException, IOException {
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
String userName = request.getParameter("username");
String password = request.getParameter("password");
String address = request.getParameter("address");
String contact = request.getParameter("contact");

RegistrationBean registrationBean = new RegistrationBean();
registrationBean.setFirstName(firstName);
registrationBean.setLastName(lastName);
registrationBean.setUserName(userName);
registrationBean.setPassword(password);
registrationBean.setAddress(address);
registrationBean.setContact(contact);

RegistrationDao registrationDao = new RegistrationDao();

String userRegistered = registrationDao.registrationUser(registrationBean);
if(userRegistered.equals("SUCCESS"))   
{
request.getRequestDispatcher("/ProductSalesPage.jsp").forward(request, 
response);
}
else
{
request.setAttribute("errMessage", userRegistered);
request.getRequestDispatcher("/RegistrationServlet.jsp").forward(request, 
response);
}
}
}

注册dao

package com.mvc.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mvc.Bean.RegistrationBean;
import com.mvc.RegistrationDB.RegistrationDB;

public class RegistrationDao {

public String registrationUser(RegistrationBean registrationBean)
{
String firstName = registrationBean.getFirstName();
String lastName = registrationBean.getLastName();
String userName = registrationBean.getUserName();
String password = registrationBean.getPassword();
String address = registrationBean.getAddress();
String contact = registrationBean.getContact();

Connection con = null;
PreparedStatement preparedStatement = null;

try
{
con = RegistrationDB.createConnection();
String query = "insert into 
users(SlNo,firstName,lastName,userName,password,address,contact) values 
(NULL,?,?,?,?,?,?)"; 
preparedStatement = con.prepareStatement(query); 
preparedStatement.setString(1, firstName);
preparedStatement.setString(2, lastName);
preparedStatement.setString(3, userName);
preparedStatement.setString(4, password);
preparedStatement.setString(5, address);
preparedStatement.setString(6, contact);

int i= preparedStatement.executeUpdate();

if (i!=0) 
return "SUCCESS"; 
}
catch(SQLException e)
{
e.printStackTrace();
}
return "Oops.. Something went wrong there..!";  
}
}

注册数据库

package com.mvc.RegistrationDB;

import java.sql.Connection;
import java.sql.DriverManager;

public class RegistrationDB {

public static Connection createConnection()
{
Connection con = null;
String url = "jdbc:mysql@localhost:3306/project/users"; 
String username = "root"; 
String password = "password"; 
try 
{
try 
{
Class.forName("com.mysql.jdbc.Driver");  
} 
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
con = DriverManager.getConnection(url, username, password); 
System.out.println("Printing connection object "+con);
} 
catch (Exception e) 
{
e.printStackTrace();
}
return con; 
}
}

注册Bean

package com.mvc.Bean;

public class RegistrationBean 

{
private String firstName;
private String lastName;
private String userName;
private String password;
private String address;
private String contact;

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getContact() {
return contact;
}
}

任何人能提供的关于为什么这个servlet不能运行的任何信息都将不胜感激。eclipse抛出的错误消息非常一般,但问题似乎基本上是将注册信息写入数据库。我可以加载注册页面并输入信息,if语句可以工作,因为页面不会转发空白文本输入框或少于6个字符的密码。问题是当您提交时,它抛出http状态404,并表示请求的资源不可用。

izkcnapc

izkcnapc1#

可能是因为您在registrationservlet中检索的参数名称不正确。
例如:您正在检索 request.getParameter("firstname") 但你的意见 name="firstName" . 注意大写的“n”(javaee服务器对uri区分大小写)。
在registrationservlet中尝试以下操作:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String firstName = request.getParameter("firstName"); // upper case N
    String lastName = request.getParameter("lastName"); // upper case N
    String userName = request.getParameter("userName"); // upper case N
    String password = request.getParameter("password");
    String address = request.getParameter("address");
    String contact = request.getParameter("contact");
    //...

相关问题