How to integrate between two httpServlets? - java

I wrote an HttpServlet and I named it "Stick" and then defined a Class named "John" in it.
In addition, I wrote another HttpServlet and named it "StickDetails".
Both of the Servlets are in the same package.
I want StickDetails servlet to function as a Main function (I want to use the "Stick" class inside it).
The problem starts when I try to write the command in StickDetails "John j = new John;"
but john isen't recognized! (""String cannot resolved to a type"")
what did I do wrong here?
Here you see StickDetails servlet:
package wood;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StickDetails extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException,ServletException {
Stick a; // Stick is not recognized "String cannot resolved to a type"
}
}
I am using Eclipse IDE for java Developers

You Need To define a new class John, don't put it Stick Servlet.
In your Stick Servlet you can create instance of John Class(if required) and after your logic, set attributes which are required in StickDetails Servlet and then use RequestDispatcher in Stick Servlet to forward request to StickDetails.

Related

Project java with two packages, but a java.lang.ClassNotFoundException between the two

I have a intellij project, with two packages.
One of them call a list in the second.
The DB connection is ok (i put sysout everywhere to follow my request)
The problem happens when the class which is supposed to screen the List from the manager of the other package call it.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.out.println("on est dans le servlet pour afficher les voitures");
CarManager cm = new CarManager();
List<Car> cars = cm.lister();
The problem happens at 'CarManager cm = new CarManager();'
it shows : java.lang.ClassNotFoundException: org.example.CarManager
however, is it imported in my servlet : import org.example.CarManager; and i have no error in the IDE (red or something)
I tried to do a "javac" in the folder for carmanager and i had errors.
Problem is : i have no idea why or how to fix it.
If the Car Manager class in another package then you cannot include its functionality just by basic approach:
className objectName= new className();
for that to work you must first import it
import packagename.classname;
make sure that the class you want to import is public otherwise it will not work
Eg: Suppose I have 2 packages a and b. I want to import package b's class x's function sayHello() in a
I first make both the class and function to be imported as public and then in an I import it using:
import b.x;
then where I want to use the function I write:
x obj = new x();
obj.sayHello()
I found how to do it, but totally by luck.
I changed my artifact in run configuration, just to try and it's ok!

Issues with ApplicationHttpRequest and getParameter

I have recently changes our app from Java 1.7 and Tomcat 7 to Java 1.8 and Tomcat 8.
The issue i am facing is that when one servlet call another the request parameter is now an ApplicationHttpRequest instead of the HttpServletRequest as it was before. This should not be an issue but for some different behaviour for the same methods.
For example request.getParameter("param1") will return a null for the HttpServletRequest if the parameter is not in the request.
But for ApplicationHttpRequest it with throw an exaction like so; java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
If the parameter does exist in the request then they behave the same.
I have written a utility method to handle the issue. But i was wondering if there is an configuration change i can do to get the original HttpServletRequest back?
public static String getRequestParameter(HttpServletRequest request, String key) {
if (request == null || StringUtils.isBlank(key)) return null;
try {
return request.getParameter(key);
} catch (ClassCastException e1) {
}
return null;
}
ApplicationHttpRequest comes with Tomcat from Apache and is a wrapper which extends HttpServletRequestWrapper. Try changing the dynamic web module version to an earlier version.
It is also possible that you have got your IDE to automatically import the wrong classes.
Here is a full list of classes which should be imported to ensure that HTTPServletRequest is used:
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

search data from bean using JAX-RS Search issue

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchContext;
import com.f2s.bean.Recentbean;
#Path("search")
public class SearchResource {
private List<Recentbean> ra;
#Path("book")
#GET
public List<Recentbean> findBooks(#Context SearchContext searchContext)
{
SearchCondition<Recentbean> condition = searchContext.getCondition(Recentbean.class);
return condition.findAll(ra);
}
error :- annotated with GET of resource, class com.f2s.restws.SearchResource, is not recognized as valid resource method.
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
First of all, you "question" is actually not a question. You're just presenting an error that you get. No explanation, no expected behaviour, no question asked. Providing some more details would be helpful.
Secondly, have you had a look on the other questions dealing with the presented exception? Here there are a few:
Exception :com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Jersey: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Errors$ErrorMessagesException when using Jersey in Java
Because of my reputation I couldn't add this as a comment...

Calling HttpServlet class from xpages client side script and regular notes forms?

I am new (again, have not touched it in a while) to Java.
I have a simple (much more complex one is planned) HttpServlet class that I am tryng to call from a webpage either from a regular Notes form or csjs on an xPage.
package com.pnc.cld;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet
{
private static final long serialVersionUID = -2950148158748149L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("doGet: Hello World!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("doPost: Hello World!");
}
}
I found this article here.
Which is orginally in Chinese so it makes it a bit hard to follow but from what I have been able to glean from it, you need a add a IServletFactory class which maps the servlet to your a url so it can be called in your browser.
But I am getting errors. This article fixed some of them
But I still still get a number of errors. One of them:
The type ServletFactory must implement the inherited abstract method IServletFactory.getServletMatch(String, String) ServletFactory.java
Also the article seems to say that you need to add com.ibm.xsp.adapter.servletFactory to the services directory but I can't find the file anywhere on my PC.
Are there any better articles or hopefully working example for calling an httpservlet out there?
Maybe you should study the sample database from Sven's blog post. Switch the Domino Designer to the Java perspective and have a look at the "Code/Java" section. There you find 3 files. One of them handles the URLs and maps them to the servlet. The third one is stored within the META-INF folder and defines where the IServletFactory should lookup the ServletFactory class (the second file). The first file ist the servlet itself.
But careful: we just got stuck with errors in the sample database, too. Pretty simple cause: in the servlet code a "static" exception is thrown :-D Remove that code and you are fine. We tested this on a 8.5.3 machine but I am sure it will do on older releases, too.
Bruce,
on Domino you need to implement a servlet OSGi style. Go steal the code from my webDAV for Domino project on OpenNTF. The servlet is definitely working!

How to extend a protected method in Clojure

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import clojure.lang.RT;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
public class Clojure4Vaadin extends AbstractApplicationServlet {
#Override
protected Class<? extends Application> getApplicationClass()throws ClassNotFoundException {
return Application.class;
}
.... Some code .....
}
How to write this in Clojure?
I'm trying to write the vaadin srvlet class in clojure:
http://dev.vaadin.com/wiki/Articles/ClojureScripting
I think what you're looking for is the following:
(def Clojure4Vaadin
(proxy [com.vaadin.terminal.gwt.server.AbstractApplicationServlet] []
(getApplicationClass [] com.vaadin.Application)))
Have a look at the documentation of proxy.
The code you have given above is used to serve a webapp written in clojure via the vaadin framework.
This code is meant to be run as Java Servlet as it is and the webapp logic would be in the clojure code (test.tlp), you would have to compile the servlet and package it with the clojure script in the webapp root directory.
Regards,
Shanmu

Categories