Thank you for your help.
500 error when calling servlet from jsp.
ClassNotFoundException appears and
Is the web.xml configuration or jsp incorrectly specifying the URL in the form?
I thought it was the source that was able to call the servlet from jsp without any problems and
By comparison, there was no difference in the description of web.xml.
I would appreciate your advice.
Below are the source code and error messages.
index.jsp
<%@pagecontentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>Pakutter</title>
</head>
<body>
<h1>Welcome to Pakutter!!</h1>
<form action="/pakutter/servlet/Login" method="Post">
Username: <input type="text" name="name"><br>
Password :<input type="text" name="pass"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Login.java
package servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.LoginLogic;
import model.User;
public class Login extensions HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost (HttpServletRequest request, HttpServletResponse response)
US>throws ServletException, IOException {
// Retrieving Request Parameters
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// Generating User Instances (User Information)
User user = new User(name, pass);
// login process
LoginLogic loginLogic = new LoginLogic();
boolean isLogin=loginLogic.execute(user);
// Successful Login Action
if(isLogin){
// Save user information to session scope
// Retrieving an HttpSession Instance
HttpSession session=request.getSession();
// Save instance to session scope
session.setAttribute("loginUser", user);
}
// Forward to login results screen
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/LoginResult.jsp");
dispatcher.forward (request, response);
}
}
/pakutter/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi: schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4" >
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>servlet.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>
/servlet/Login
</url-pattern>
</servlet-mapping>
</web-app>
Error Message
HTTP Status 500 - Internal Server Error
Type Exception Report
Error initializing message servlet class servlet.Login
Description The server encountered an unexpected condition that presented it from filling the request.
exception
javax.servlet.ServletException—Error initializing servlet class servlet.Login
org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:478)
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:80)
org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:624)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process (AbstractProtocol.java:861)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun (NioEndpoint.java:1455)
org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java:49)
java.util.current.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)
java.util.current.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunable.run(TaskThread.java:61)
java.lang.Thread.run (Thread.java:745)
cause
java.lang.ClassNotFoundException: servlet.Login
org.apache.catalina.loader.WebappClassLoaderBase.loadClass (WebappClassLoaderBase.java:1285)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass (WebappClassLoaderBase.java:1119)
org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:478)
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:80)
org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:624)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process (AbstractProtocol.java:861)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun (NioEndpoint.java:1455)
org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java:49)
java.util.current.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)
java.util.current.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunable.run(TaskThread.java:61)
java.lang.Thread.run (Thread.java:745)
Below is the code that worked properly.
The operation was to get the value entered in index.jsp on the servlet side of Result.java and perform the calculation, so there was no ClassNotFoundException and I was able to perform the process.
index.jsp
<%@pagelanguage="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<metacharset=UTF-8">
<title>Movie Fee Calculation</title>
</head>
Result.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.announcement.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// @WebServlet ("/Result")
public class Result extensions HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost (HttpServletRequest request, HttpServletResponse response)
US>throws ServletException, IOException {
intage=-1;
// Get request parameters
request.setCharacterEncoding("UTF-8");
String gender = request.getParameter("gender");
age = Integer.parseInt(request.getParameter("age"));
String type = request.getParameter("type");
// Generate an instance of a Calc class
Calc calc = new Calc();
// substitute calculation results
int result=calc.movieFeeCalc(age,gender,type);
request.setAttribute("result", result);
getServletConfig().getServletContext().getRequestDispatcher("/WEB-INF/jsp/result.jsp").forward(request,
response);
}
}
<body>
<h1>Calculate movie rates!</h1>
<form action="/movieFee/servlet/Result" method="Post">
Gender: <br>
Man <input type="radio" name="gender" value="0" required>
Woman <input type="radio" name="gender" value="1">br>
Age: <br>
<input type="number" name="age" required><br>
Type: <br>
3D<input type="radio"name="type"value="0"required>
2D<input type="radio"name="type"value"1">
<input type="submit" value="To Calculate">
</body>
</html>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi: schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4" >
<servlet>
<servlet-name>Result</servlet-name>
<servlet-class>servlet.Result</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Result</servlet-name>
<url-pattern>
/servlet/Result
</url-pattern>
</servlet-mapping>
</web-app>
java.lang.ClassNotFoundException: servlet.Login
It is said that
/pakutter/WEB-INF/classes/servlet/Login.class
This is probably because the file is not located.What do you think?
© 2024 OneMinuteCode. All rights reserved.