Get client operating system name - servlets

I am trying to get the operating system name of a client in a servlet using the following code:
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userAgentString = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentString);
OperatingSystem os = userAgent.getOperatingSystem();
}
}
I am getting an error UserAgent cannot be resolved to a type. How can i solve this?

Related

Micronaut Servlet support: discovery of classic HttpServlet implementations

I followed the micronaut-servlet guide using Jetty Server and everything worked as expected (micronaut 2.0.0.M3, micronaut-servlet 1.0.0.M3).
However, for our Proof-of-Concept we need to migrate several classic HttpServlet (v3.1) based implementations to micronaut. Example:
#WebServlet(name = "Hello",
urlPatterns = {"/hello/*"},
initParams = {#WebInitParam(name = "name", value = "World")})
public class HelloServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().format("<h1>Hello %s!</h1><p>session=%s</p>",
getInitParameter("name"), request.getSession(true).getId()
).flush();
}
}
However, this is not picked up by Micronaut. Is this not supported? If not, what is the best way to migrate this to idiomatic micronaut?

Use request.getRequestDispatcher(myurl).forward(request,response) but not working

When I use request.getRequestDispatcher(myurl).forward(request, response) in servlet to jump to another HTML page, I find it doesn't work. A response can be seen in web browser but the page just doesn't change.
I tried response.sendRedirect(myurl) then, and it also doesn't work.
Here is my code:
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) {
System.out.println("get");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.sendRedirect("/ForwardTest/new.html");
// request.getRequestDispatcher("/new.html").forward(request, response);
System.out.println("success");
}
}
Each time I run this, just get a "success" output in console. That means getRequestDispatcher had run, but not correctly.

How can i display a view with annotations

Normally i used to display my views in javaee with the "normal" kind of servlet like this :
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.getServletContext().getRequestDispatcher("/WEB-INF/views/myview.jsp").forward(request, response);
}
But i'm on a project where there is only annotation everywhere and i don't understand how can i display some view this way ...
#RequestScoped
#Path("/user")
#Produces("application/json")
public class UserController extends Controller{
#Path("v1/{pseudo}")
#GET
public String getUser(#PathParam("pseudo") String pseudo){
...
Can someone help me ?
Thx
Sorry my google searsh was bad and i found my finnaly answer :
#Path("/documentation")
public class DocumentationController extends HttpServlet{
#GET
public void getHome(#Context HttpServletRequest request,
#Context HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/views/test.jsp")
.forward(request, response);
}
}
thx for the response anyway

Wildcard path mapping /A/* should return 404 when path info is absent like /A

I am new in Servlets. I am trying to map a URL using the wildcard (*), but it's not working the way i expected.
Here is my servlet class.
#WebServlet(urlPatterns = {"/A/*"})
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Working...");
}
}
The above servlet is working for both example.com/A and example.com/A/car.
I want to work the servlet only for the the second option which is example.com/A/whatEver. How can i do that ?
In simple: I just want to work the servlet if there's anything after example.com/A.
Any help will greatly appreciated.
Just invoke HttpServletResponse#sendError() with SC_NOT_FOUND (404) when HttpServletRequest#getPathInfo() is null or empty or equals to /.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// ... (continue)
}

How can i send response to a request without having HttpServletresponse

Hi please some one help me in this issue.
I have a class which extends HttpServlet and a method called doGet(HttpServletRequest request)
But i dont have a HttpServletResponse .Now i want to respond to a request from a client as im not having response how can i respond to a request.Can i create able to create it dynamically in that class using reflections and is their any other way..My problems pseudo code looks as follows
protected void doGet(HttpServletRequest request)throws ServletException, IOException {//having only request as parameter in doGet()
//Want to respond to a jsp with out having a response
//how can i create a response object if i got a HttpServletResponse response the code looks as follows
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
if(name.equals("James")&& password.equals("abc")){
response.sendRedirect("result.jsp");
}
else{
pw.println("u r not a valid user");
}
}
There is no such method protected void doGet(HttpServletRequest request) throws ServletException, IOException
The method signature is:
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException

Resources