gpt4 book ai didi

java - 小服务程序 :The server encountered an internal error that prevented it from fulfilling this request

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:28 29 4
gpt4 key购买 nike

我使用 jsp 页面和 servlet 创建了注册页面,但出现错误:描述 服务器遇到内部错误,无法满足此请求。

exception

java.lang.NullPointerException
com.javatpont.RegisterUser.register(RegisterUser.java:13)
com.javatpont.CreateServlet.doGet(CreateServlet.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是 --->Register.jsp 页面

<form  name=F1 onSubmit="return dil(this)" action="CreateServlet">
<table height="350" cellspacing="5" cellpadding="3">
<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>RE-PASSWORD:</td><td> <input type="password" name="repassword"/></td>
</tr>
<tr>
<td>PHONE:</td><td> <input type="text" name="phone"/></td>
</tr>
<tr>
<td>ADDRESS:</td><td> <textarea rows=4 cols=40 wrap=virtual name="adderess"></textarea></td>
</tr>
<TR>
<TD> Select your designation:</TD>
<TD>
<SELECT NAME="syd">
<option>student
<option>professor
<option>Associate-Professor
<option>lecturer
<option>non-teaching staff
</SELECT>
</TD>
</TR>
<tr><td></td><td><input type="submit" value="Submit"/>
<INPUT TYPE=RESET VALUE="CLEAR"></td></tr>
</table>
</form>

这是 CreateServlet.java

    package com.javatpont;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.Naming;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class CreateServlet extends HttpServlet {

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

response.setContentType("text/html");
PrintWriter out=response.getWriter();

String username=request.getParameter("username");
System.out.println("-----------------------"+username);
String password=request.getParameter("password");
String repassword=request.getParameter("repassword");
String ph=request.getParameter("phone");
double phone=Double.parseDouble(ph);

String adderess=request.getParameter("adderess");

String syd=request.getParameter("syd");

// line number 33 int status=RegisterUser.register(username, password, repassword,phone, adderess,syd);
System.out.println("***********************"+username);

if(status>0){
System.out.println("==========================="+username);
request.setAttribute("welcome","WELCOME! YOU HAVE BEEN REGISTERD");
RequestDispatcher rd=request.getRequestDispatcher("member.jsp");
rd.include(request, response);
}
else{
out.print("Sorry,Registration failed. please try later");
RequestDispatcher rd=request.getRequestDispatcher("member.jsp");
rd.include(request, response);
}

out.close();
}

}

这是 RegisterUser.java

 package com.javatpont;
import java.sql.*;
public class RegisterUser {
static int status=0;
//int accountno=1;
public static int register(String username,String password,String repassword,double phone,String adderess,String syd){
//public static int register(String email,String password,String gender,String country,String name){

Connection con=GetCon.getCon();
PreparedStatement ps;

try {
System.out.println("here is the problem line number 13");
//line number13-> ps = con.prepareStatement("Insert into NEWMEMBER values(?,?,?,?,?,?,?)");
int nextvalue1=GetCon.getPrimaryKey();
ps.setInt(1,nextvalue1);
ps.setString(2,username);
ps.setString(3,password);
ps.setString(4,repassword);
ps.setDouble(5,phone);
ps.setString(6,adderess);
ps.setString(7,syd);

status=ps.executeUpdate();
System.out.println(status);
} catch (SQLException e) {

e.printStackTrace();
}
return status;

}
}

这是--> GetCon.java

public class GetCon {
private GetCon(){}

public static Connection con;
static{
try {
Class.forName(DBIntializer.DRIVER);
con=DriverManager.getConnection(DBIntializer.CON_STRING,DBIntializer.USERNAME,DBIntializer.PASSWORD);
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {

System.out.println("Exception in GetCon");
}

}
public static Connection getCon(){
return con;
}



public static int getPrimaryKey(){
int nextvalue=0;
Connection con=GetCon.getCon();
PreparedStatement ps2;
try {

ps2=con.prepareStatement("select ff from dual");

ResultSet rs=ps2.executeQuery();
rs.next();
nextvalue=rs.getInt(1);



} catch (SQLException e) {

e.printStackTrace();
}
return nextvalue;

}
}

最佳答案

嗨,我认为您的实际问题就在这里

Connection con=GetCon.getCon();

您能够获得连接并仍然尝试执行此操作..

ps = con.prepareStatement(...)

我们需要首先找出连接失败的原因..检查您提供的URL是否像这样“jdbc:mysql://localhost/DATABASE_NAME”..localhost(如果数据库位于您的计算机本身或该 ip 上)。还要确保类路径中有所需的 jdbc 驱动程序。

编辑:

查看最新的注释和代码,看起来像是缺少 jar 问题。您需要将连接器库添加到运行时类路径`

java -cp .;mysql-connector-java-5.1.25-bin.jar GetCon` 

我的示例使用 Windows 类路径分隔符“;”,在其他系统上可能有所不同(在 Linux 上为“:”)。它还假设 mysql-connector-java-5.1.25-bin.jar 位于同一文件夹中。如果不是这种情况,请输入库的路径而不是普通名称。

关于java - 小服务程序 :The server encountered an internal error that prevented it from fulfilling this request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29456999/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com