Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java web error in the servlet file

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 370
    Answer it

    Hello,

    I have a problem to run the next code. I have a glassfish server and I add servlet api. The problem is when I try to run it this error occurs: In-place deployment at /Users/jakublemiszewski/NetBeansProjects/JspServletTutorial/build/web GlassFish Server, deploy, null, false /Users/jakublemiszewski/NetBeansProjects/JspServletTutorial/nbproject/build-impl.xml:1077: The module has not been deployed. See the server log for details.

    The other error: /Users/jakublemiszewski/NetBeansProjects/JspServletTutorial/src/java/Servlet/SimpleServlet.java:47: error: method getAttribute in interface HttpSession cannot be applied to given types; session.getAttribute("savedUserName", userName); required: String found: String,String reason: actual and formal argument lists differ in length 1 error /Users/jakublemiszewski/NetBeansProjects/JspServletTutorial/nbproject/build-impl.xml:899: The following error occurred while executing this line: /Users/jakublemiszewski/NetBeansProjects/JspServletTutorial/nbproject/build-impl.xml:321: Compile failed; see the compiler error output for details. BUILD FAILED (total time: 0 seconds)

    I'm new in this stuff so I don't understand what is the problem Hope to receive some help. Thank you :) The code:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
     import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     *
     * @author Jakub Lemiszewski
     */
    
    public class SimpleServlet extends HttpServlet {
    
        /**
         * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
         * methods.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
    
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            try (PrintWriter out = response.getWriter()) {
                String userName = request.getParameter("name");
                HttpSession session = request.getSession();
                ServletContext context = request.getServletContext();
                if (userName != "" && userName != null)
                {
                    session.getAttribute("savedUserName", userName);
                    context.setAttribute("savedUserName", userName);
                }
                /* TODO output your page here. You may use following sample code. */
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet SimpleServlet</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Servlet SimpleServlet at " + request.getContextPath() + "</h1>");
                 out.println("<h2>Parameter Requested " + (String) userName + "</h2>");
                  out.println("<h3>Session User Name " + (String) session.getAttribute("savedUserName") + "</h1>");
                  out.println("<h4>Context User Name " + (String) context.getAttribute("savedUserName") + "</h4>");
                  out.println("<h5>Defult Usert Config " + getServletConfig().getInitParameter("defaultUser") + "</h5>"); 
                    // defaultUser cames from web.xml -> servlets -> Add Parameters
                out.println("</body>");
                out.println("</html>");
            }
        }
    
        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /**
         * Handles the HTTP <code>GET</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /**
         * Handles the HTTP <code>POST</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /**
         * Returns a short description of the servlet.
         *
         * @return a String containing servlet description
         */
        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>
    
    }
    

 1 Answer(s)

  • Hi Jake
    you are getting error in below code :

     if (userName != "" && userName != null)
    {
    session.getAttribute("savedUserName", userName);
    context.setAttribute("savedUserName", userName);
    }
    

    Update above code as below :

     if (userName != "" && userName != null)
    {
    session.setAttribute("savedUserName", userName);
    context.setAttribute("savedUserName", userName);
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: