我不熟悉JSP和servlet。我正在tomcat版本10.0中设计一个库管理系统,当用户以html形式使用他的用户ID和密码登录时,一旦凭据匹配,它就会将其发送到登录servlet,并使用RequestDispatcher将用户ID发送到userlogin(home)JSP以供进一步使用。
在此之前,一切正常,在JSP中有两个操作,一个是用户可以检查MySQL数据库中的图书可用性和他/她的图书的归还日期,如果他/她借了任何图书,否则不会显示图书借阅消息。
任务是,我想通过ajax获得上述两个操作的结果。一旦用户在输入字段中输入要搜索的图书名称并按下按钮,ajax应调用servlet并从数据库中获取与之对应的值,以htmltable格式创建数据并将其返回给ajax调用,ajax在获取时应使用innerhtml或DOM html函数将其显示在特定的div中。用户也可以使用相同的方法获取用户借来的图书的归还日期。
问题:
但一旦在字段和按钮中输入了书名,就会调用该操作,但它显示了405个错误,即servlet不允许GET方法,尽管在ajax调用中使用了eventhough,我的servlet使用了POST方法。我注意到,当用户登录时,URL栏显示登录servlet URL,而页面显示JSP页面,并且一旦调用ajax,数据将从登录servlet发送(http://localhost:8080/library/UserLogin)并在同一登录servlet中接收(http://localhost:8080/library/UserLogin?search=java)这似乎在get方法中起作用(但我在两侧都使用了POST),所以我猜数据仍然在同一个servlet URL中移动。
用户登录。JAVA
public class UserLogin extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
//getting userid and password for checking with database//
String userid=request.getParameter("uid");
String pass=request.getParameter("pwd");
try{
Connection connect=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","password");
PreparedStatement ps =connect.prepareStatement("select * from library.user where userid = ? and pass = ?");
ps.setString(1, userid);
ps.setString(2, pass);
ResultSet rs=ps.executeQuery();
if(rs.next()){
request.setAttribute("userid",userid);
RequestDispatcher rd=request.getRequestDispatcher("userlogin.jsp");
rd.forward(request,response);
}
else{
RequestDispatcher rd=request.getRequestDispatcher("http://localhost:4200/user");
rd.forward(request,response);
}
}catch(SQLException e){
System.out.println(e);
}
}}
'
用户登录。jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
<link rel="stylesheet" href="css/app.css">
<link rel="icon" href="images/logo.jpg" type ="image/x-icon">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="heading">
<div class="title">
<h1>Online Library</h1>
</div>
<div class="logo">
<i class="fas fa-book-reader fa-7x"></i>
</div>
</div>
<div class="search-container">
<form>
<input type="text" name="search" id="search" placeholder="Search by book genre (Eg:Search Java for Java books)" />
<button id="getbook"><i class="fas fa-search"></i></button>
<!--the button for ajax to get book availability table-->
</form>
</div>
<hr>
</div>
<button id="mybook">My Books and Due date</button>
<!--the button for ajax to get user's book returndate table-->
<%String suserid=(String)request.getAttribute("userid");
out.println("name:"+suserid);%> <!--storing userid for using it for user's book returndate-->
<div id="books"> <!--div tag where the table will show-->
</div>
<script>
$(document).ready(function(){
$("#getbook").click(function(){
var search=$("#search").val()
$.ajax({
type:'POST',
url:'Issuedbooks',
data:search,
success:function(response){
console.log(response);
$("#books").html(response);
},error:function(request,status,error){
alert("Error:"+error+status);
}
});
});
$("#mybook").click(function(){
var userid= suserid; /*I also doubt on this declaration if it is wrong give suggestion please*/
$.ajax({
type:'POST',
url:'mybooks',
data:userid,
success:function(response){
console.log(response);
$("#books").html(response);
},error:function(request,status,error){
alert("Error:"+error+status);
}
});
});
});
</script>
</body>
</html>
发行的书。JAVA
public class Issuedbooks extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
out.println("<html><body>"); //creating a html table//
String genre=request.getParameter("search"); //the value in input field of jsp page//
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","password");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select * from library.issue where genre='"+genre+"'");
out.println("<table border=1>");
out.println("<tr><th>Bookid</th><th>Returndate</th><th>Bookname</th><th>Authorname</th></tr>"); //creating table header//
while(rs.next()){
String bookid=rs.getString(3);
String returndate=rs.getString(5);
out.println("<tr><td>"+bookid+"</td><td>"+returndate+"</td>");
}
Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","password"); //as I have to get data from two database I have created two connections//
Statement stmt1 = con1.createStatement();
ResultSet rs1=stmt1.executeQuery("select * from library.books where genre='"+genre+"'");
while(rs1.next()){
String bookname=rs1.getString(2);
String authorname=rs1.getString(3);
out.println("<td>"+bookname+"</td><td>"+authorname+"</td></tr>"); //continuing to add this in same table//
}
out.println("</table");
out.println("</body></html>"); //end of htmltable//
}catch(SQLException e){
e.printStackTrace();
}
}}
我的书。JAVA
public class mybooks extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
out.println("<html><body>");
String userid=request.getParameter("suserid"); //getting userid from jsp which is obtained from userlogin servlet//
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","password");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select * from library.issue where userid='"+userid+"'");
out.println("<table border=1>");
out.println("<tr><th>Bookid</th><th>IssueId</th><th>Userid</th><th>Bookid</th><th>Issuedate</th><th>Returndate</th><th>Genre</th></tr>");
if(rs.next()){
String issueid=rs.getString(1);
String userid=rs.getString(2);
String bookid=rs.getString(3);
String issuedate=rs.getString(4);
String returndate=rs.getString(5);
String genre=rs.getString(7);
out.println("<tr><td>"+issueid+"</td><td>"+userid+"</td><td>"+bookid+"</td><td>"+issuedate+"</td><td>"+returndate+"</td><td>"+genre+"</td></tr>");
}else{
out.println("No books issued");
}
}catch(SQLException e){
e.printStackTrace();
}
}}
这些是我的代码,在运行这些代码时,我会遇到如下错误。我的tomcat服务器中的此url不支持HTTP GET。我使用POST方法做了所有事情,因为我在使用GET方法时遇到了错误,jsp页面中的url仍然显示userlogin servlet url,并且在调用ajax时数据没有通过该url。我已经在网上查过了。xml一切正常。
对不起,描述太长了,我只想清楚地描述我的问题以得到答案。提前感谢:)
很抱歉再次共享代码,但我不知道我在哪里丢失了该部分。这里我只分享了不起作用的部分
用户登录。jsp
<%String userid=(String)request.getAttribute("userid");%> //converted to received object to string here//
<input type="hidden" id="userid" value='<%out.println(userid);%>'> //set value of input as userid and hide it so I can use it for ajax//
<input type="button" id="mybook" value="My books and due date" />
<div id="books">
</div>
<script>
$("#mybook").click(function(){
console.log("test print");
var userid= $("#userid").val();
$.ajax({
type:'POST',
url:'mybooks',
data:{user_id:userid.toString()}, //I have a doubt whether it is changed to string or not so again tried to transfer it as string//
success:function(response){
console.log(response);
$("#books").html(response);
},error:function(request,status,error){
alert("Error:"+error+status);
}
});
});
</script>
我的书。JAVA
public class mybooks extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
out.println("<html><body>");
String userid=request.getParameter("user_id");
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","password");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select * from library.issue where userid='"+userid+"'");
out.println("<table border=1>");
out.println("<tr><th>Bookid</th><th>IssueId</th><th>Userid</th><th>Bookid</th><th>Issuedate</th><th>Returndate</th><th>Genre</th></tr>"); //i am getting upto this content in response//
while(rs.next()){
String issueid=rs.getString(1);
String uid=rs.getString(2);
String bookid=rs.getString(3);
String issuedate=rs.getString(4);
String returndate=rs.getString(5);
String genre=rs.getString(7);
out.println( "<tr><td>"+issueid+"</td><td>"+uid+"</td><td>"+bookid+"</td><td>"+issuedate+"</td><td>"+returndate+"</td><td>"+genre+"</td></tr>"); //I didn't get this part in response, I doubt whether the data is receiving in servlet evethough it is passed here//
}out.println("</table>");
out.println("</body></html>");
}catch(SQLException e){
e.printStackTrace();
}
}}
问题内容: 编辑:整个问题原来是网络问题,但是如果您看到有关如何优化流程的任何想法,我仍然会感激不尽。 我对Servlets还是很陌生,在不久的将来,我遇到了一个与性能有关的问题。我正在尝试通过Google Chrome浏览器中的XHR对象发送视频文件。视频文件存储在Blob对象中。我在JavaScript脚本中使用了此功能: 它运行良好,因为Blob到达Servlet,在这里我使用以下代码对其进
如何执行上述动作? 我读过类似的文章 我将其映射到login servlet `
问题内容: 我有一个JSP文件为 jsp 1.jsp ,另一个JSP文件为 jsp 2.jsp 我已经包括 JSP 2.jsp 在 JSP 1.jsp页面 使用 现在,我需要某些元素上的click事件。在那件事上,我想将一个字符串变量传递给包含的jsp。 假设我有一个列表,单击它后,我想将该列表的名称转移到另一个JSP, 在另一个JSP中,我试图使用该字符串执行某些任务。 我在没有任何servle
我正在构建一个简单的web应用程序,并尝试创建一个登录页面。该页面由一个带有加载servlet的表单的JSP组成。 在servlet中: 这段代码可以工作,但它在URL字符串中包含了用户名和密码,所以这显然不是一个好的做法。我尝试使用POST来实现这一点,但我得到了一个错误。(HTTP状态405-此URL不支持HTTP方法POST) 我需要知道如何使用POST将参数从JSP发送到Servlet。我
我正在和其他同学完成一个项目,我们被卡住了。在我们的登录servlet中,我们连接到数据库并检索与电子邮件匹配的数据 我们需要这样传递信息:数据库--- 谢谢 LogInServlet 配置文件Servlet 配置文件HTML
我在servlet里有一段代码 我有一个ajax代码 我有一个jsp代码 如何通过ajax将servlet中list的值分配给jsp中的allProduct