使用java中的SOAP Web服务创建Web应用程序。当我通过服务器端的测试客户端进行测试时,注册功能正常工作,但是在客户端,当我填写详细信息并单击signUp按钮时,同一页面正在刷新,但没有执行任何操作(数据未获得插入数据库)。调试时,我看到代码失败了
if(qdone.equalsIgnoreCase("true")){
有谁可以帮我解决这个问题。我不知道我哪里错了。
这是我的代码
signUp.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link href="Flat-UI-master/css/flat-ui.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link rel="shortcut icon" href="Flat-UI-master/images/favicon.ico">
<link rel="stylesheet" href="Flat-UI-master/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="css/icon-font.css">
</head>
<body>
<div id="page-wrapper">
<header class="header-11">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="row">
<div class="navbar-collapse collapse in" style="height: auto;">
<ul class="nav navbar-nav">
<li class="active"><a href="index1.html"><span class="glyphicon glyphicon-home"></span>Home</a></li>
<li><a href="#explorer">College Explorer</a>
<li><a href="#fundraiser">Fund Raisers</a></li>
<li><a href="#fafsa">FAFSA</a></li>
<li><a href="#studentloan">Student Loans</a></li>
</ul>
<ul class="nav pull-right">
<li><a class="btn btn-primary" href="signIn.jsp">SIGN IN</a> </li>
</ul>
</div>
</div>
</div>
</div>
</header>
<br>
<br>
<br>
<section class="header-11-sub bg-midnight-blue">
<div class="background"> </div>
<div class="container">
<div class="row">
<div class="col-sm-4">
<h3>Task Tracker</h3>
<p>Make your life easy having a task tracker</p>
<div class="signup-form">
<form id="form" method="post" action="signUp">
<div class="form-group">
<input class="form-control" type="text" placeholder="username"
id="username">
</div>
<div class="form-group">
<input class="form-control" type="password"
placeholder="password" id="password">
</div>
<div class="form-group">
<input class="form-control" type="text" placeholder="fname"
id="fname">
</div>
<div class="form-group">
<input class="form-control" type="text" placeholder="lname"
id="lname">
</div>
<div class="form-group">
<input class="form-control" type="text"
placeholder="email@example.com" id="email">
</div>
<div class="form-group">
<button type="submit" class="btn btn-large btn-primary">Sign
Up</button>
<a href='http://localhost:8080/FinalClient/View/signUp.jsp'
class="btn btn-large btn-primary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</div>
<script class="cssdeck"
src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script class="cssdeck"
src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js">
</script>
</body>
</html>
signUp Servlet(signUp.java)
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jws.WebService;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import DefaultNamespace.ServiceProxy;
/**
* Servlet implementation class signUp
*/
@WebServlet("/signUp")
public class signUp extends HttpServlet {
private static final long serialVersionUID = 1L;
ServiceProxy proxy =new ServiceProxy();
/**
* @see HttpServlet#HttpServlet()
*/
public signUp() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out =response.getWriter();
response.setContentType("text/html");
String qdone;
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String email = request.getParameter("email");
if (!checkMandatorySignUpFields(username, password, fname, lname, email)) {
request.setAttribute("message", "Required Field Missing!");
String nextJSP = "/View/signUp.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}else {
proxy.setEndpoint("http://localhost:8080/Final/services/Service");
System.out.println("crossed the endpoint");
qdone=proxy.signUp(username,password,fname,lname,email);
System.out.println(qdone);
System.out.println("signup qdone");
HttpSession session =request.getSession();
System.out.println("Session started");
if(qdone.equalsIgnoreCase("true")){
System.out.println("qdone value obtained....signup successful");
session.setAttribute("userSession", session);
out.println("Welcome to CollegeTime Task planner" +username);
request.setAttribute("message", "Signup successful, Please login.");
String nextJSP= "/View/signIn.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}else{
out.println(qdone.equalsIgnoreCase("false"));
out.println("\n <a href='http://localhost:8080/FinalClient/View/signUp.jsp'><br>Go back to Signup and try again</a>");
request.setAttribute("message", "Unable to Signup!" + qdone);
String nextJSP = "/View/signUp.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
private boolean checkMandatorySignUpFields(String username, String password, String fname, String lname, String email){
// TODO Auto-generated method stub
return true;
}
}
服务器端的Service.java(注册功能)代码
@WebService
public class Service {
DatabaseConnection db = new DatabaseConnection();
String result="";
public String signUp(String username, String password, String fname, String lname, String email){
System.out.println("Inside SignUp");
result=db.signUp(username, password, fname, lname, email);
System.out.println("SignUp Successful");
System.out.println(result);
return result;
}
服务器端signUp的数据库查询实现
public String signUp(String username, String password, String fname, String lname, String email){
int rowcount=0;
String result = "";
Calendar cal= Calendar.getInstance();
String time = sdf.format(cal.getTime());
try{
String query="select * from user where username = '"+username+"'";
ResultSet res;
stm.executeQuery(query);
res=stm.getResultSet();
if(!res.next()){
String insertQuery = "insert into user(username, fname, lname, password, email, lastlogin) values ('"+username+"', '"+password+"','"+fname+"','"+lname+"','"+email+"','"+time+"')";
rowcount=stm.executeUpdate(insertQuery);
if(rowcount>0){
result ="true";
}
else{
result="false";
}
}
}catch(SQLException e){
e.printStackTrace();
}
System.out.println(result);
return result;
}
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FinalClient</display-name>
<welcome-file-list>
<welcome-file>signUp.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>signUp</display-name>
<servlet-name>signUp</servlet-name>
<servlet-class>servlets.signUp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>signUp</servlet-name>
<url-pattern>/View/signUp</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
我的问题已经解决了。只是张贴,以防将来有人遇到同样的问题。
我在signUp.jsp表单中给出了name属性
以前在signUp.jsp中:
<form id="form" method="post" action="signUp">
<div class="form-group">
<input class="form-control" type="text" placeholder="username"
id="username">
</div>
signUp.jsp中的稍后(解决方案)(添加名称属性):
<form id="form" method="post" action="signUp">
<div class="form-group">
<input class="form-control" type="text" placeholder="username"
id="username" name="username">
</div>