2017-03-26 93 views
0

我有一個網絡項目與NetBeans的Maven。我正在使用Glassfish & MySQL。我的Java servlet不工作在maven(netbeans)

這是我的註冊畫面 newUserScreen

點擊「TAMAM」之後,我想借此從網站註冊到我的數據庫,但我只是看到我的服務器URL,並沒有發生任何事情。 emptyUserServletUrlScreen

這是我的servlet:

package tr.kasim.cc.servlet; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.ArrayList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
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 tr.kasim.cc.app.Application; 
import tr.kasim.cc.util.GeneralUtil; 
import tr.kasim.cc.model.SignUp; 

/** 
* 
* @author SelmanKasim 
*/ 


@WebServlet(urlPatterns = {"/userservlet"}) 
public class SignUpUsersServlet extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     doProcess(req, resp); 
    } 

    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     doProcess(req, resp); 
    } 

    protected void doProcess(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     PrintWriter out = null; 
     try { 
      out = new PrintWriter(resp.getOutputStream()); 

      String action = req.getParameter("action"); 
      if ("addUser".equals(action)) { 
       addUser(req, resp); 
      } 

     } catch (Exception ex) { 
      Logger.getLogger(SignUpUsersServlet.class.getName()).log(Level.SEVERE, null, ex); 
      System.out.println("hata var agacim"); 
     } finally { 
      if (out != null) { 
       out.flush(); 
       out.close(); 
      } 
     } 
    } 

    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws Exception, IOException { 

     String loginName = req.getParameter("loginName"); 
     String password = req.getParameter("password"); 
     String eMail = req.getParameter("eMail"); 
     String userName = req.getParameter("userName"); 
     String userDate = req.getParameter("userDate"); 
     String userJob = req.getParameter("userJob"); 
     String userTelephone = req.getParameter("userTelephone"); 
     String userCity = req.getParameter("userCity"); 
     String userGenderId = req.getParameter("userGenderId"); 

     SignUp sign_up = new SignUp(null,loginName,password,eMail,userName,userDate,userJob,userTelephone,userCity,userGenderId); 

     Application.getApp().getMainService().addUser(sign_up); 
     resp.sendRedirect("./userList.jsp"); 
    } 

} 

這是我註冊的jsp:

<%-- 
    Document : userKayit 
    Created on : 24.Mar.2017, 17:42:39 
    Author  : SelmanKasim 
--%> 


<%@page import="java.util.List"%> 
<%@page import="tr.kasim.cc.app.Application"%> 
<%@page import="tr.kasim.cc.model.SignUp"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>User Kayit</title> 
    <link rel="stylesheet" type="text/css" href="css/index.css"></link> 
</head> 

<body><center> 
    <p class="title">Yeni User Kayit</p> 

    <form method="post" action="userservlet"> 
     <input type="hidden" name="action" value="addUser"/> 
     <table> 
      <tr> 
       <td>Giris Adi:</td> 
       <td><input type="text" name="girisAdi"/></td> 
      </tr> 
      <tr> 
       <td>Sifre:</td> 
       <td><input type="text" name="sifre"/></td> 
      </tr> 
      <tr> 
       <td>eMail:</td> 
       <td><input type="text" name="eMail"/></td> 
      </tr> 
      <tr> 
       <td>Kullanici Adi:</td> 
       <td><input type="text" name="kullaniciAdi"/></td> 
      </tr> 
      <tr> 
       <td>Kullanici Kayit Tarihi:</td> 
       <td><input type="text" name="kullaniciKayitTarihi"/></td> 
      </tr> 

      <tr> 
       <td>Kullanici Cinsiyeti:</td> 
       <td><input type="text" name="kullaniciCinsiyeti"/></td> 
      </tr> 
      <tr> 
       <td>Kullanici İsi:</td> 
       <td><input type="text" name="kullaniciIsi"/></td> 
      </tr> 
      <tr> 
       <td>Kullanici Telefonu:</td> 
       <td><input type="text" name="kullaniciTelefonu"/></td> 
      </tr> 
      <tr> 
       <td>Kullanici Sehri:</td> 
       <td><input type="text" name="kullaniciSehri"/></td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
       <td><input type="submit" value="Tamam"/></td> 
      </tr> 
     </table> 
     <br/><br><br> 
     <a href="index.jsp">Ana Sayfaya Dön</a><br> 
     </center> 
    </form> 

</body> 


</html> 

現在我不能採取任何登記。 你覺得呢?我的錯誤在哪裏/什麼是?

+1

你有任何web.xml嗎?如果是的話,添加它 – developer

+0

不,我沒有任何web.xml – superior

回答

0

首先你的註解是錯誤的。將其更改爲

@WebServlet(name = "SignUpUsersServlet", urlPatterns = {"/userservlet"}) 

它應該可以正常工作。

其次,您需要在您的servlet代碼中包含requestDispatcher。

變化

resp.sendRedirect("./userList.jsp"); 

RequestDispatcher rd = request.getRequestDispatcher("userList.jsp"); 
rd.forward(request, response); 

三擺脫你doProcess和ADDUSER方法只有用的doGet和doPost工作。

如果你想有doProcess和addUser 你在doGet或doPost中調用addUser你只能在那裏調用doProcess。

+0

對不起,但我不明白你的第三部分。如果我扔我的addUser部分,我不能添加任何新用戶,我只嘗試第一部分和第二部分,我不工作了。你能解釋更多嗎? – superior

+0

你的重定向不起作用,這就是爲什麼你看到空白頁面。您不會在doGet或doPost上調用addUser。 –