简体   繁体   中英

My java servlet is not working at maven (netbeans)

I have a web project with maven at netbeans. I'm using glassfish & MySQL.

This is my registration screen newUserScreen

After clicking "Tamam", I want to take a registration from web to my database but I am just seeing my servlet url and nothing is happening. emptyUserServletUrlScreen

This is my 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");
    }

}

This is my registration 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>

Now I could not take any registration. What do you think? Where/What is my error?

First your annotation is wrong. Change it to

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

and it should work fine.

Second you need to include requestDispatcher in your servlet code.

Change

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

to

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

Third get rid of your doProcess and addUser methods and work only with doGet and doPost.

And if you want to have doProcess and addUser where are you calling addUser in doGet or doPost you are only call doProcess there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM