如何使用JSP解决此问题?

时间:2019-05-28 11:13:23

标签: eclipse jsp tomcat

我尝试使用apache tomcat部署Web应用程序,并且遇到异常。我对Java完全陌生,所以请帮助我。 消息org.apache.jasper.JasperException:无法为JSP编译类。

说明服务器遇到意外状况,阻止其满足请求。 进入我的登录页面的Java代码如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%@ page import ="javax.swing.JOptionPane;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Login</title>
    </head>
<body>

<%
    //connection to database

    Connection conn = null;
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","***"); 
    Statement st= conn.createStatement();
    String username=request.getParameter("username");
    String password=request.getParameter("userpass");

    //Check if username or password is empty

    if(username!="" && password!="")
    {
        session.setAttribute("username",username);
        //session.setAttribute("refreshCount","0");
        ResultSet rs=st.executeQuery("select * from login where userId='"+username+"'");
        if(rs.next())
        {
            //Check if user password matches with database password

            if(rs.getString(2).equals(password))
            {
                //retrieving the role of user form the database 1-Manager and 0 - student

                int role=rs.getInt(4);

                if(role==1)
                {
                    response.sendRedirect("managerHome.jsp");
                }
                else
                {

                    response.sendRedirect("studentHome.jsp");
                }

            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid username or password.");
                String redirectURL= "index.jsp";  
                    response.sendRedirect(redirectURL);  

            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid username or password.");
            String redirectURL= "index.jsp";  
                response.sendRedirect(redirectURL);  

        }

    }
    else
    {
        JOptionPane.showMessageDialog(null, "Username or password should not be empty");
                String redirectURL= "index.jsp";  
                    response.sendRedirect(redirectURL);  
    }
%>

</body>
</html>

例外:

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to compile class for JSP
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:604)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:422)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

根本原因:

org.apache.jasper.JasperException: Unable to compile class for JSP
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:619)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

java.lang.IllegalArgumentException: Page directive: invalid value for import
org.apache.jasper.compiler.Node$PageDirective.validateImport(Node.java:619)
org.apache.jasper.compiler.Node$PageDirective.addImport(Node.java:599)
org.apache.jasper.compiler.Parser.parsePageDirective(Parser.java:366)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:478)
org.apache.jasper.compiler.Parser.parseFileDirectives(Parser.java:1797)
org.apache.jasper.compiler.Parser.parse(Parser.java:141)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244)
org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:127)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:202)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:385)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:362)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:346)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

下面是我的xml文件 Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" metadata-complete="true" version="3.0">
  <display-name>SEProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ManagerTimesheetsServlet</display-name>
    <servlet-name>ManagerTimesheetsServlet</servlet-name>
    <servlet-class>timesheet.ManagerTimesheetsServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ManagerTimesheetsServlet</servlet-name>
    <url-pattern>/ManagerTimesheetsServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>StudentTimesheetServlet</display-name>
    <servlet-name>StudentTimesheetServlet</servlet-name>
    <servlet-class>timesheet.StudentTimesheetServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>StudentTimesheetServlet</servlet-name>
    <url-pattern>/StudentTimesheetServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>StudentUpdateServlet</display-name>
    <servlet-name>StudentUpdateServlet</servlet-name>
    <servlet-class>timesheet.StudentUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>StudentUpdateServlet</servlet-name>
    <url-pattern>/StudentUpdateServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>CreateTimesheetServlet</display-name>
    <servlet-name>CreateTimesheetServlet</servlet-name>
    <servlet-class>timesheet.CreateTimesheetsServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CreateTimesheetServlet</servlet-name>
    <url-pattern>/CreateTimesheetsServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <display-name>ApplyLeaveHandler</display-name>
    <servlet-name>ApplyLeaveHandler</servlet-name>
    <servlet-class>timesheet.ApplyLeaveHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ApplyLeaveHandler</servlet-name>
    <url-pattern>/ApplyLeaveHandler</url-pattern>
  </servlet-mapping>
  <servlet>
    <display-name>EditLeaveHandler</display-name>
    <servlet-name>EditLeaveHandler</servlet-name>
    <servlet-class>timesheet.EditLeaveHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>EditLeaveHandler</servlet-name>
    <url-pattern>/EditLeaveHandler</url-pattern>
  </servlet-mapping>
  <servlet>
    <display-name>SubmitUpdatedLeaveHandler</display-name>
    <servlet-name>SubmitUpdatedLeaveHandler</servlet-name>
    <servlet-class>timesheet.SubmitUpdatedLeaveHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SubmitUpdatedLeaveHandler</servlet-name>
    <url-pattern>/SubmitUpdatedLeaveHandler</url-pattern>
  </servlet-mapping>
  <servlet>
    <display-name>UpdateLeaveHandler</display-name>
    <servlet-name>UpdateLeaveHandler</servlet-name>
    <servlet-class>timesheet.UpdateLeaveHandler</servlet-class>
  </servlet>
  <servlet>
    <display-name>ViewLeaveHandler</display-name>
    <servlet-name>ViewLeaveHandler</servlet-name>
    <servlet-class>timesheet.ViewLeaveHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UpdateLeaveHandler</servlet-name>
    <url-pattern>/UpdateLeaveHandler</url-pattern>
  </servlet-mapping>
</web-app>

2 个答案:

答案 0 :(得分:2)

错误java.lang.IllegalArgumentException: Page directive: invalid value for import本身表明您有import类不存在。即<%@ page import ="javax.swing.JOptionPane;" %> <-,在您导入;的结尾处,remove可以正常工作。

答案 1 :(得分:0)

我知道这不是您问题的答案,但相信我,这可以使您免于很多麻烦(自己进行测试)。

尝试不要在JSP页面中使用原始Java代码,而是可以在服务器(.java文件)上完成工作,并在JSP页面上返回变量和数组,并使用JSTL使用该变量构建页面。

How to avoid Java code in JSP files?