I'm running Apache Tomcat 8 on Eclipse Mars on Windows 7, and here's my piece of code:
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;
#WebServlet("/Main")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
public Main() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("");
pw.println("");
pw.println("");
pw.println("<h1>Example 0</h1>");
pw.println("");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
It starts tomcat, ok, and "Example 0" can be seen when I access localhost. BUT, if I change the string in the response to "Example 1", it takes around 10 min to change it. I'm a beginner in Servlet / Tomcat development, and I'm failing to grasp the reason why the delay occurs. Why that happens??
Not an answer, but I recommend you code web stuffs with Netbeans, there is no doubt Eclipse is the king, but Netbeans does web stuffs better.
Related
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed last month.
I am learning how to use servlet and i copied the example in my book but i keep getting a 404 error. here is the code :
package servlet;
import java.io.IOException;
import` java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
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 SampleServlet2
*/
#WebServlet("/SampleServlet2")
public class SampleServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String[] luckArray = {"超スッキリ","スッキリ","最悪"};
int index = (int) (Math.random()*3);
String luck=luckArray[index];
Date date= new Date();
SimpleDateFormat sdf=new SimpleDateFormat("MM月dd日");
String today=sdf.format(date);
response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>スッキリ占い</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>"+today+"の運勢は「"+luck+"」です</p>");
out.println("</body>");
out.println("</html>");
}
}
``
I don't really know what to do as the book doesn't include a 404 error and all the topics about servlet are old.
There is no problem in your code.
may be the problem is with your server configuration.
I am using Eclipse IDE and tomcat 9 , and your code is working fine.
This question already has answers here:
#WebServlet fails in Netbeans 11.0 with java.lang.RuntimeException: com.example.NewServlet.<init>(NewServlet.java:1)
(2 answers)
Closed 2 years ago.
Im using Tomcat 9.0.30 and JDK 8 and Netbeans 11.3.
Somehow all my Web-Applications do not start anymore and I did not change anything. They all throw the same exception
Here I auto-generated a servlet and only forwarded a JSP and it still does not work.
package servlets;
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;
#WebServlet(name = "ExamController", urlPatterns = {"/ExamController"})
public class ExamController extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.getRequestDispatcher("examView.jsp").forward(request, response);
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
}
When I start this class I get this exception:
I fixed it by downgrading my Netbeans to Version 11.1.
I think that Tomcat 9.0.30 and Netbeans with a version higher than 11.2 are not compatible.
In NetBeans / Java - exception breakpoint (for Throwable) does not catch exceptions (RuntimeException in code of library for servlet). If you search tediously, you can find a purple stop line but without looking into the state of variables, etc. VS.NET does not have such ailments - it stops where you need it and everything is clear.
Is this a NetBeans problem or Java? How to get rid of breakpoint problems?
In here example would give you basic understanding of Exception Handling in Servlet
Actually putting a break point you can not catch a servlet exception because of it is happening before reaching the end point then you have to do something like this
When a servlet generates an error developers can handle those exceptions in various ways
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;
#WebServlet("/SampleExceptionHandler")
public class SampleExceptionHandler extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
exceptionProcessor(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
exceptionProcessor(request, response);
}
private void exceptionProcessor(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// Analyze the servlet exception
Throwable throwable = (Throwable) request
.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
String servletName = (String) request
.getAttribute("javax.servlet.error.servlet_name");
if (servletName == null) {
servletName = "Unknown";
}
String requestUri = (String) request
.getAttribute("javax.servlet.error.request_uri");
if (requestUri == null) {
requestUri = "Unknown";
}
}
}
I'm trying to use JavaBeans or the first time and I'm getting the following
error:
javax.naming.NameNotFoundException: DemoEJB
javax.naming.NamingException: Lookup failed for
'java:global/DemoEJB/ContenedorEJB/BeanStateful!beanssesion.BeanStatefulLocal'
in SerialContext[myEnv={java.naming.factory
I'm using NetBeans with glassfish 4.1 server.
The application is really simple, I have an Enterprise application project where I link an EJB Module (called ContenedorEJB) and a Client (which is a Servlet).
This is the projects structure:
ClienteWeb
Source Packages
-servlets
-ServletEJB.java
ContenedorEJB
-Source Packages
beanssesion
BeanStateful.java
BeanStatefulLocal.java
DemoEJB
-Java EE Modules
ContenedorEJB.jar
ClienteEJB.war
The servlet code is as follows bellow. The only thing I do it's look up the sessionBean,
c.lookup("java:global/DemoEJB/ContenedorEJB/BeanStateful!beanssesion.BeanStatefulLocal");
save it, and then use the implemented methods getValor and setValor just to see if the values I send to the EJB Module are saved. But it gets lost because it can't find the BeanStateful even though it's there.
package servlets;
import beanssesion.BeanStatefulLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletEJB extends HttpServlet {
BeanStatefulLocal beanStateful = lookupBeanStatefulLocal();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ServletEJB</title>");
out.println("</head>");
out.println("<body>");
out.println("Enviamos un valor al EJB Stateful: ");
beanStateful.setValor("Valor desde Servlet");
out.println("Valor devuelto " + beanStateful.getValor());
out.println("</body>");
out.println("</html>");
}
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
private BeanStatefulLocal lookupBeanStatefulLocal() {
try {
Context c = new InitialContext();
return (BeanStatefulLocal) c.lookup("java:global/DemoEJB/ContenedorEJB/BeanStateful!beanssesion.BeanStatefulLocal");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
}
Anyone can help?
As you are using GlassFish 4.1 you will find life simpler if you put all of your classes (including EJBs) in your web module for the time being.
Secondly you rarely need to use explicit JNDI lookups anymore; use injection instead:
public class ServletEJB extends HttpServlet {
#javax.ejb.EJB
BeanStatefulLocal beanStateful;
...
}
BUT, there is a big problem with using stateful session beans like this. This servlet instance will be shared with all clients and so will the EJB that you are injecting/looking up. This will lead to serious concurrency issues.
I'm attempting to create a login process using JSPs and linking them to my MySQL database. The issue I have encountered is an internal error (500) in my browser after I click "login".
I have setup my web.xml for the servlet ValidateLogin.java and I know that is not the issue. Can someone please help me here? I'll place down my code below that involves my HtmlServlet Request and Response.
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ValidateLogin extends HttpServlet {
Connection connect;
ResultSet result;
String username, password, query;
DatabaseConnection database_connection;
protected void processRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UF-8");
PrintWriter out = response.getWriter();
try {
// Code Here...
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
}
Nevermind, I worked out the issue...
Change code from to...
//FROM
response.setContentType("text/html;charset=UF-8");
//TO
response.setContentType("text/html");