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

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 ?

Related

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

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);}}
}

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

what values can a JSP form "action" take?

What can a JSP form action be?
I have a Login.jsp page for the user to end the details.
Can i give the servlet class in the form action?
here is the the servlet code.
package mybean;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
System.out.println("In the Login Servlet");
LoginBean user = new LoginBean();
user.setUemail(request.getParameter("uemail"));
user.setUpass(request.getParameter("upass"));
user = LoginDAO.login(user);
if(user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("LoginSuccess.jsp");
}else
response.sendRedirect("LoginFailed.jsp");
} catch (Throwable exc)
{
System.out.println(exc);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
As per the specs , it can take any valid URI
This attribute specifies a form processing agent. User agent behavior for a value other than an HTTP URI is undefined.
Can i give the servlet class in the form action ?
Yes if the servlet class name resolves to a valid URL mapped in the web.xml or else you will encounter a 404 .
Let us consider your JSP is at the root of the application, then
<FORM action="someServletName" method="post">
Now this will be resolved as protocol://servername:port/context/someServletName .Now somewhere you should have a mapping for /someServletName , either in web.xml or through annotation to a Servlet or JSP.
<servlet>
<servlet-name>someServletName</servlet-name>
<servlet-path>packageName.servletName</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>someServletName</servlet-name>
<url-pattern>/someServletName</url-pattern>
</servlet-mapping>

eclipse delete servlet with web.xml entries

am using Eclipse's New->Servlet wizard thereby having auto-generated servlet and servlet-mapping entries ready for me. But when I select a servlet's java file and delete it, the corresponding entries in web.xml don't get deleted.
How do I do this?
I would imagine just edit the web.xml file and delete the entries. It's likely by design that they are not automatically deleted when you delete the servlet.
How about using Annotations? You don't have to take care of any configuration in web.xml for this.
package com.inventwheel.servlet;
import java.io.IOException;
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 DeleteMe
*/
#WebServlet(description = "DeleteMe Servlet", urlPatterns = { "/DeleteMe" })
public class DeleteMe extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public DeleteMe() {
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
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

How can I redirect to another page in Servlet using response.sendRedirect() method?

**loginServlet.java**
package com.anil.apps;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
String uid=request.getParameter("userid");
String password=request.getParameter("pwd");
if(uid.equals("Anil")&&password.equals("missinlx")){
//out.println("welcome "+uid);
response.sendRedirect("welcomeUser?userid="+uid);
}
else{
out.println("invalid username or password");
}
}
}
**WelcomeUserServlet.java**
package com.anil.apps;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html><body><h1>Welcome " +uid+ "</h1></body></html>");
}
}
what is wrong with my code?? I want to redirect the page to welcomeUser.java page using response.sendRedirect() method.
As I am new to servlet please help me out of it. please tell me the whole format for the page redirection in Servlets.
GET parameters are not turned in to variables automatically. You need to add this to the top of your doGet method in the WelcomeUserServlet:
String uid = request.getParameter("userId");
Making that class look like this:
public class WelcomeUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uid = request.getParameter("userId");
PrintWriter out=response.getWriter();
out.println("<html><body><h1>Welcome " +uid+ "</h1></body></html>");
}
}
You are using the wrong argument for the sendRedirect method
You should use a complete path like
response.sendRedirect("some/path/here/to/"+welcomeUser?userid="+uid)
Or better
response.sendRedirect(response.encodeURL(response.getContextPath()+"welcomeUser?userid="+uid))
Or use a request dispatcher which knows the structure of your project and does not need a full path
request.getRequestDispatcher("welcomeUser?userid="+uid).forward(request,response)