Hot questions for Using GlassFish in netbeans 8

Question:

On Windows 7 I downloaded the 'netbeans-8.0.1-javaee-windows.exe' installer from this site https://netbeans.org/downloads/. The installer installs GlassFish 4.1, Java 1.8.0_20 and NetBeans 8.01. After installation, whenever I try to start the GlassFish server from within the NetBeans Services area, I get this error:

Could not start GlassFish Server 4.1: HTTP or HTTPS listener port is occupied while server is not running

So I used

netstat -ano | find "1527"

to find out which process is holding this port and as it seems it is the Java Derby database itself that was just started by the GlassFish process. So the Glassfish startup is complaining about something it just caused itself. Strange. I don't know what to do. Anybody any idea?

Thanks already.


Answer:

I also had this problem, it is because there is an application LISTENING to 8080 port. To solve this problem I followed the below steps:

  1. Open cmd.exe then type

    netstat -aon | find ":8080" | find "LISTENING"

  2. You will see like this result

    TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1464

  3. Copy PID "1464".

  4. Open Task Manager (Ctrl+Alt+del), go to the details tag, then find the program or service via PID that is listening to the port 8080 then STOP it or End process.

Question:

I'm learning JavaEE CDI and I created a small application with NetBeans 8.0+Glassfish 4. After upgrading to NetBeans 8.0.1 and Glassfish 4.1 I'm getting a lot of errors reporting that some packages does not exist. For example, I cannot use the following code because I'm getting the message that package javax.enterprise.event does not exist.

package jlacerda;

import javax.inject.Inject;
import javax.enterprise.event.Event;

public class CMensagem 
{
    @Inject
    private Event<Evento> gerarEvento;

    public String getMensagem()
    {
        return "Hello world!";
    }

    public void gerarEvento()
    {
        Evento evento = new Evento();
        evento.setMensagem("Objeto criado a partir da classe CMensagem");

        gerarEvento.fire(evento);
    }
}

This situation also occurs with packages:

import javax.enterprise.inject.Alternative;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

If I change the server to Glassfish 4.0 the same code runs as expected and all the packages are correctly imported.

I searched in the NetBeans and Glassfish forums but I did not found any situation like this one.

Thanks in advance for any sugestion that might help me solving this situation.


Answer:

With NetBeans 8.0.x you need only to open Project Properties, then go to Libraries section and add "Java EE Web 6 API Library". I have the same issue when migrated from NetBeans 7.4 to 8.0, but the solution is quite simple and obvious.

Question:

When i specify the directory of Glassfish 5.0, i get this message in the IDE :

No usable default domain. Use Next to create a personal domain.

But, there is already a domain (domain1) and i don't want to create another one.

My question is : How to solve this problem ?! Or how can i make Netbeans find the existing domain ?


Answer:

After a lot of research, it turned out, that, to be able to perform such task, you have to run NetBeans 8.2 as Administrator (under Windows). If you do so, the "No usable default domain. Use Next to create a personal domain." will not be displayed.

I hope it can help other people.

Question:

This is noob question but still...

My localhost port 8181 is working but when I run my web application via Netbeans IDE, the default port is 8080. How can I change the default port to 8181?


Answer:

It looks like NetBeans 8 reads the default port from http-listener-1 in your domain.xml.

To change it, you have to assign a different port to http-listener-2, because this one uses port 8181 by default.

To do that you have different options:

A)

You can open the Glassfish Admin UI via http://localhost:4848.

Navigate to server-config -> Network Config -> Network Listeners -> http-listener-2 and change the port to something like 8282 (8080 is currently in use by http-listener-1).

Now do the same for http-listener-1 and change the port to 8181.

B)

You can also change it manually directly in the domain.xml, shutdown Glassfish before you start.

Open /glassfish_installation/glassfish/domains/domain1/config/domain.xml with a text-editor and search for 8080. There should be something like:

 <network-listener port="8080" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool"></network-listener>
 <network-listener port="8181" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool"></network-listener>

Change that to:

 <network-listener port="8181" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool"></network-listener>
 <network-listener port="8080" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool"></network-listener>

C)

You can use asadmin:

asadmin set configs.config.server-config.network-config.network-listeners.network-listener.http-listener-2.port=8282

asadmin set configs.config.server-config.network-config.network-listeners.network-listener.http-listener-1.port=8181

(This works for Glassfish v4, for other versions you may have to adjust the "config path".)

The final step of these solutions is to restart Netbeans and you should be done.

See also:

  • netbeans starts running web applications on port 8080, where can i change it?
  • How to run glassfish 4 on port 80 instead of 8080. root acess is not an issue.?

Question:

I am new to java spring and glassfish 4.1 .. my application is deploying successful in glassfish but browsing error 404 ..

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">


    <servlet>

    <servlet-name>springapp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>

  </servlet> 
  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping> 
    <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list> 
</web-app>

Answer:

Change

<url-pattern>*.htm</url-pattern>

to

<url-pattern>/*</url-pattern>

or use URL for the specific html file.

Question:

while practising servlet programs i encountered this problem. and now i am facing this same problem in whichever project i create . dont know what i am doing wrong. i am using netbeans 8.1 and glass fish server . the error is :-

HTTP Status 500 - Internal Server Error
type Exception report
messageInternal Server Error
descriptionThe server encountered an internal error that prevented it from      
fulfilling this request.
exception javax.servlet.ServletException: Error instantiating servlet class calc
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class calc

root cause

java.lang.NoSuchMethodException: calc.<init>()

note The full stack traces of the exception and its root causes are available in the   
GlassFish Server Open Source Edition 4.1 logs.
GlassFish Server Open Source Edition 4.1 

html code:-

<html>
    <head>
       <title>TODO supply a title</title>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
       <form method="get" action="http://localhost:8080/calculator/calc">
           <label for="no1">Enter the number one</label>
           <input type="text" id="no1" name="no1"/><br><br>
           <label for="no2">Enter the number two</label>
           <input type="text" id="no2" name="no2"/><br><br>
           <select name="opr">
               <option>addition</option>
               <option>subtraction</option>
               <option>multiplication</option>
               <option>Division</option>
           </select>
           <input type="submit" value="submit"/>
           <input type="reset" value="reset"/>

       </form>
    </body>
</html>

servlet code:-

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
class calc extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {
       PrintWriter pw=res.getWriter();
       float result=0;
       float a=Float.parseFloat(req.getParameter("no1"));
       float b=Float.parseFloat(req.getParameter("no2"));
       String opr=req.getParameter("opr");
       if(opr.equals("addition"))
       {
           result=a+b;
       }
       if(opr.equals("subtraction"))
       {
           result=a-b;
       }
       if(opr.equals("multiplication"))
       {
           result=a*b;
       }
       if(opr.equals("division"))
       {
           result=a/b;
       }
       pw.println("the answer is "+result);
    }
}

xml code:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
       <servlet-name>calc</servlet-name>
       <servlet-class>calc</servlet-class>
    </servlet>
    <servlet-mapping>
       <servlet-name>calc</servlet-name>
       <url-pattern>/calc</url-pattern>
    </servlet-mapping>
    <session-config>
       <session-timeout>
           30
       </session-timeout>
    </session-config>
    <welcome-file-list>
       <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

Answer:

Your serlvet class must extend the javax.servlet.http.HttpServlet class,

public class calc extends the javax.servlet.http.HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {
    .....

For your second error, I,m not sure why are you getting this error could be another config for inject objects, but to solve this problem you must create a public constructor with no arguments.

public class calc extends the javax.servlet.http.HttpServlet
    {
        public calc() {}
        public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
        {
    .....

Question:

I learn jsp using netbeans 8 and GlassFish Server. I have MyLog.java

package MyClass;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class MyLog {

    private static final SimpleDateFormat TIME_FMT= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    private static PrintWriter log=null;
    public MyLog(String logpath) throws IOException{
        log=new PrintWriter(new FileWriter(logpath,true));   
    }

    public static synchronized void println(String s)
    {
        log.println(TIME_FMT.format(new java.util.Date())+" - "+s);
        log.flush();
    }
    public static synchronized void close(){
    log.close();
    }
}

and my class usage is

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="MyClass.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Switch the log ON</title>
    </head>
    <body>
        <% 
            MyLog log =(MyLog) application.getAttribute("logfile");

            if (log==null)
            {
                try{
                    log=new MyLog("log/mylog.log");
                    application.setAttribute("logfile", log);
                    log.println("Logging enabled");
                    out.println("Logging enabled");
                }
                catch (Exception e) {
                    out.println(e.getMessage());
                }
            }
            else 
            {
                log.println("Attempt to enable logging");
                out.println("Logging was already enabled");
            }
        %>
    </body>
</html>

so i get error that couldn't found file "log/mylog.log".

I created log/mylog.log (with directory) in web folder and even in application folder, but it cannot find this file.

I tried to use

new File (filename).exists () in debug to my existing files but it always gives false.

So my question is, where I should place my files so that netbeans (or glassfish) could find it?


Answer:

The look-up is always done in your current working directory which once your app is deployed in a container depends on the target application server. But, even if found, you really can't depend on that or you'll app will not remain portable anymore.

So, you can move your log directory inside the WEB_INF directory of your web application and access the absolute directory location at runtime through the ServletContext like

log = new MyLog(application.getRealPath("/WEB-INF/log/mylog.log"));

Warning: You must store your log directory inside WEB-INF or they'll become accessible through the browser for the whole world wide web to see.

Another alternative if you wouldn't want your log files to go inside the web app's directory or you have some other central location for all logs on that server is to configure any absolute directory location you like as a context parameter in your web.xml.

<web-app>
  ...
  <context-param>
    <param-name>LogPath</param-name>
    <param-value>/absolute/log/dir/path/file.log</param-value>
  </context-param>

The value can then be accessed through ServletContext again.

log = new MyLog(application.getInitParam("LogPath"));

For casual logging, another alternative would be to just use the logging capabilities that come with the Servlet spec already implemented by your container. The below will log an error message and the stack trace of the associated exception to a log file, the name and location of which will depend on your servlet container but will be available in its documentation.

application.log("Error message", throwable);

On Tomcat, the logs should go to ${TOMCAT_HOME}/logs/catalina.out by default.


There's no virtual path here as such. Both the paths are relative; with getRealPath() you're just resolving that relativeness (if that's a word) against you web application's root directory. With your approach too the relative path was getting resolved but against your app server's current working directory. It's just that you weren't able to find where it is. It's not a fixed location and changes with different app servers or even different versions of the same server.