Categories
discuss

How to set up tomcat context path for web application? .war file name is coming infront of the application name in the url

I am having a trouble getting my web application ‘xyz’ running in tomcat 7 server hosted in DigitalOcean servers at:

http://myDomain:8080/myWebApp

The name of my .war file is “.MyWebApp”.

after deploying the .war file in tomcat, the application starts at:

http://myDomain:8080/MyWebApp/myWebApp

any idea how to remove this “MyWebApp” part from the URL?

I have seen some of the solutions for this problem, they are mostly solutions of running the application in localhost. They suggested to change the ‘context.xml’ file which I already did like this:

<?xml version='1.0' encoding='utf-8'?>
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

     path="" docBase="MyWebApp" debug="0" reloadable="true"
</Context>

Any help would be very much appreciated!

Do not mark this as duplicate. I have checked all the solutions which worked for me in localhost, but not in digital ocean VPS. I think I am pretty specific here.

Thank you in advance!

Answer

I had to add this line in in the server.xml file. Tod do this:

nano etc/tomcat/server.xml

add this line:

 <Context path="" docBase="Advocatoree" debug="0" reloadable="true"></Context>

Save the file.

Restart tomcat.

Categories
discuss

SearchView in one activity, results in another one

My use case is the following: in activity A, I have an action bar with a collapsible SearchView. When the user gives her query and presses the ‘search’ button, I would like to show activity B with the results. I’m failing to do so, here is my code:

searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:hint="@string/action_search_hint"
            android:label="@string/app_name"/>

Activity A:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

AndroidManifest for Activity A and B:

    <activity
        android:name=".ActivityA">

        <intent-filter>

            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>

        </intent-filter>

    </activity>

    <activity android:name=".ActivityB">

        <intent-filter>

            <action android:name="android.intent.action.SEARCH"/>

        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"/>

    </activity>

Howerver, no matter what I try, getSearchableInfo in A always returns null.

I have the feeling I’m not understanding something. The only way I can make getSearchableInfo() return not null is when I put the SEARCH intent filter and the searchable meta-data into activity A in the manifest. But then, when I press the ‘search’ button, another instance of activity A is started again on top of the current one, which definitely is not what I want. I guess I could add singleTop to A and handle the SEARCH action in onNewIntent and start B, but I generally don’t want A to be singleTop, I want the default. Or do I?

I tried adding this to A in the manifest (as suggested in some other threads):

    <meta-data
        android:name="android.app.default_searchable"
        android:value=".ActivityB" >
    </meta-data>

What am I doing wrong?

EDIT: I replaced the ‘getComponentName()’ line in A with the following:

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));

and it seems to work the way I want! Am I doing something wrong here, or is this the correct way? I’m a bit uncertain because it is not mentioned anywhere!

Answer

According to Android documentations, SearchManager.getSearchableInfo(componentName) takes one argument, which is:

componentName: The activity to get searchable information for

In your case you need ActivityB, so constructing a ComponentName that points to ActivityB is the correct way

new ComponentName(this, ActivityB.class)
Categories
discuss

Jersey MessageBodyWriter not found for media type=text/plain

I’m trying to follow the Jersey docs to enable a non-200 response if an error occured (https://jersey.java.net/documentation/latest/representations.html#d0e3586)

My code looks like :

@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public ResponseBuilder getData(@FormParam("one") String one,@FormParam("two") String two,@FormParam("three") String three) {
    if(one.isEmpty() || two.isEmpty() || three.isEmpty()) {
        logger.error("Missing params for getData");
        throw new WebApplicationException(501);
    }
    return Response.ok();
}
}

This unfortunately yields the following error :

[2015-02-01T16:13:02.157+0000] [glassfish 4.1] [SEVERE] [] [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1422807182157] [levelValue: 1000] [[ MessageBodyWriter not found for media type=text/plain, type=class org.glassfish.jersey.message.internal.OutboundJaxrsResponse$Builder, genericType=class javax.ws.rs.core.Response$ResponseBuilder.]]

Answer

The problem is the return type of your method. It has to be Response instead of ResponseBuilder.

Change your code to the following and it should work:

@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response getData(@FormParam("one") String one,@FormParam("two") String two,@FormParam("three") String three) {
    if(one.isEmpty() || two.isEmpty() || three.isEmpty()) {
        logger.error("Missing params for getData");
        throw new WebApplicationException(501);
    }
    return Response.ok();
}
Categories
discuss

view.getViewTreeObserver().addOnGlobalLayoutListener leaks Fragment

When I use the GlobalLayoutListener to see if the softKeyboard is opened or not the fragment is not garbageCollected anymore after it is destroyed.

What I do:

  • I remove the Listener in the onDestroy() of my Fragment
  • I set the Listener to null in onDestroy()
  • I set the view that is observed to null in onDestroy()

Still leaks the fragment.

Does anyone had a similar issue and knows a fix for it??

My onDestroy:

   @Override
public void onDestroy(){
    Log.d(TAG , "onDestroy");

    if(Build.VERSION.SDK_INT < 16){
        view.getViewTreeObserver().removeGlobalOnLayoutListener(gLayoutListener);
    }else{
        view.getViewTreeObserver().removeOnGlobalLayoutListener(gLayoutListener);
    }

    view = null;
    gLayoutListener = null;



    super.onDestroy();
    }

Answer

I believe strongly removing the Listener, referenced by a View object, in onDestroy() is too late. This override method occurs after onDestroyView(), which supposed to “…clean up resources associated with its View.” You may use the same code in onStop() instead. Although I did not use this technique.

I can suggest this code, which I have used without any issues with the debugger.

// Code below is an example. Please change it to code that is more applicable to your app.
final View myView = rootView.findViewById(R.id.myView);
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @SuppressLint("NewApi") @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {

        // Obtain layout data from view...
        int w = myView.getWidth();
        int h = myView.getHeight();
        // ...etc.

        // Once data has been obtained, this listener is no longer needed, so remove it...
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        else {
            myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
});

Notes:

  • Since getViewTreeObserver is used for layouts, normally you need this listener for a short time only. Hence the listener is removed immediately.
  • The second call to removeOnGlobalLayoutListener() should be crossed out by the Studio since it is not available before JELLY_BEAN.
  • Pragma code @SuppressWarnings("deprecation") is not necessary if you’re using Android Studio.
  • Code myView = rootView.findViewById(R.id.myView); may need to change to a more applicable code to your app or situation.
Categories
discuss

getSupportFragmentManager().findFragmentById(R.id.map) started returning null

I have a map fragment inside another fragment. This was working before, but I think it messed up after I updated google play services library. What is wrong? fragment_map.xml:

<fragment
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

MapFragment:

GoogleMap map;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {

        view = inflater.inflate(R.layout.fragment_map, container, false);
        map = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager()
                .findFragmentById(R.id.map))
                .getMap(); // NullPointerException at this line
        map.getUiSettings().setAllGesturesEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(true);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }

    return view;
}

Answer

I had to change

getActivity().getSupportFragmentManager()

to

getChildFragmentManager()

to get it working. No idea why it was working perfectly few days ago.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..