Web Application: the requested resource is not available after Maven clean - eclipse

I have a dynamic web project that has worked perfectly fine many times, but after going to Run As > Maven clean, I tried running my application again and when trying to go to the url http://localhost:8080/Servlet_Project/AccountServlet I get an error saying the requested resource is not available. No exception is thrown - I can't seem to be able to access the servlet. Here is my AccountServlet.java. I also mapped the servlet in the web.xml but thought it was unnecessary because of the annotations.
#WebServlet("/AccountServlet")
public class AccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AccountServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/account.jsp").forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
HibernateClient hc = HibernateClient.getInstance();
Accounts a = hc.getAccount(username, password);
if(a==null){request.getRequestDispatcher("/login.jsp").forward(request, response);
}
else{
HttpSession sess = request.getSession();
sess.setMaxInactiveInterval(600);
sess.setAttribute("username", username);
sess.setAttribute("firstName", a.getFirstname());
sess.setAttribute("lastName", a.getLastname());
sess.setAttribute("address", a.getAddress());
sess.setAttribute("state", a.getState());
sess.setAttribute("country", a.getCountry());
sess.setAttribute("phone", a.getPhone());
sess.setAttribute("SSN", a.getSsn());
sess.setAttribute("email", a.getEmail());
sess.setAttribute("city", a.getCity());
String balances = (a.getBalance()).toString();
double balance = Math.round(Double.parseDouble(balances)* 100d)
sess.setAttribute("balance", balance);
request.getRequestDispatcher("/account.jsp").forward(request, response);}}
}

Related

How to access a SlingServlet by its URL?

I have created an OSGi Bundle and uploaded to a CQ5 server. This bundle contains a SlingServlet like this:
#SlingServlet(paths = { "/rest/matches" }, methods = { "POST", "GET" })
public class MatchDayRestServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 5088643736228890684L;
#Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String responseString = "Hello World!";
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write(responseString);
response.getWriter().flush();
response.getWriter().close();
}
}
Now I am stucked because I have no idea what URL I should use to make GET calls to this servlet.
My CRXDE is located at http://HOST:IP/crx/de/index.jsp

Extracting a value in a header using post-http in a restful wabservice

I'm a new rest programmer and I want to transmit the ip of the android device(Client) to the server and register it in a file. I want to use http request post for this and I have to transmit this parameter value in a header . I used
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("var", "C02G8416DRJM"));
in my main activity so as to insert this information in the header of the request(I'm not sure if it's registred in the header or the body).
However, I couldn't retrieve this value in the sever part ... I tried this
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String var= req.getParameter("var");
Writer writer1 = null;
try {
writer1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\X\\Y\\Z\\header.txt"), "utf-8"));
writer1.write(var);
} catch (IOException ex){
// report
} finally {
try {writer1.close();} catch (Exception ex) {}
}
}
Any particular reason you are using Servlet.doPost for receiving REST requests ?
JAX-RS makes it extremely simple. A resource can be defined as:
#Path("/fruit")
public class MyResource {
#GET
public String get() {
System.out.println("GET");
return Database.getAll();
}
#GET
#Path("{name}")
public String get(#PathParam("name")String payload) {
System.out.println("GET");
return Database.get(payload);
}
#POST
public void post(String payload) {
System.out.println("POST");
Database.add(payload);
}
#PUT
public void put(String payload) {
System.out.println("PUT");
Database.add(payload);
}
}
Complete sample at:
https://github.com/arun-gupta/javaee7-samples/tree/master/jaxrs/jaxrs-endpoint
Any parameters in the request can be retrieved as:
#Context Application app;
#Context UriInfo uri;
#Context HttpHeaders headers;
#Context Request request;
#Context SecurityContext security;
#Context Providers providers;
For example, all HTTP headers are available using headers field.
Complete sample showing this is at:
https://github.com/arun-gupta/javaee7-samples/tree/master/jaxrs/request-binding

Tomcat Class Not Found - Servlet

I am having a strange problem in deploying a basic web app through Eclipse and Tomcat
The error -
SEVERE: Allocate exception for servlet DeCommServlet
java.lang.ClassNotFoundException: com.authentication.DeCommServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
However, the application still gets to the DeCommServlet doPost() method and tomcat stays deployed but with that error.
Servlet:
<servlet>
<servlet-name>DeCommServlet</servlet-name>
<servlet-class>com.authentication.DeCommServlet</servlet-class>
</servlet>
<!-- Servlet Mappings -->
<servlet-mapping>
<servlet-name>DeCommServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The servlet class is the correct link but still getting this error. I have nothing in any of the lib folders, but in the application properties I have the Apache Tomcat Lib added in Build Path >> Libraries
Has anyone seen this before?
Code for DeCommServlet:
#WebServlet("/DeCommServlet")
public class DeCommServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public DeCommServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("sdf");
System.out.println("ddd");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.getUserPrincipal().getName();
response.sendRedirect("http://www.google.com");
}
New Class Path1
This is how your webapp should look like :
> DeCommGlobal
> |-->src
> |-->com
> |-->authentication
> |-->DeCommServlet.java
> |-->META-INF
> |-->WEB-INF
> |-->classes
> |-->lib
It was the deployment assembly that was causing the issue. Added the lib folder and we are good to go! Thanks

Eclipse/Tomcat7: application context root reloading returns 404

I have a servlet which is mapped to application context root using the "" string:
So when I hit the url http://127.0.0.1:8080/myapp/, I can see "This is ROOT!" in the browser. So far so good.
But if I modify something, say changing the String This is ROOT!! to ROOT!! and save the file in eclipse. Eclipse will take a couple of seconds to reload the context. Now if i hit the same url, a 404 page is displayed while all other pages still work fine. Only the root mapping is broken. So I redeploy (eclipse's "run on server") the app and the root mapping is back again. Any ideas on how can I fix this?
#WebServlet("")
public class Root extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("This is ROOT!!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
https://issues.apache.org/bugzilla/show_bug.cgi?id=54955
This is a bug of tomcat 7.0.40.
when you deploy it. Context root is accessible. But reload the app will cause context root to return 404.

Run servlet on glassfish server prompts for master password and run fails

I have written a simple HelloServlet as follows.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
#WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello</H1>\n" +
"</BODY></HTML>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I have GlassFish 3.1 installed in the Java EE perspective in Eclipse.
Whenever I try and run the servlet clicking on "run on server" on the server it comes up with the following message:
This domain requires an administrative password to be set before the
domain can be started. Please specify an administrative password. The
Master Password is required to start the domain. No console, no
prompting possible. You should either create the domain with
--savemasterpassword=true or provide a password file with the --passwordfile option.
Command start-domain failed.
There is always a default master password "changeit" for glassfish. Why is it prompting me with this message ?