Glassfish5和servlet的问题

时间:2018-03-31 08:55:38

标签: java jpa servlets glassfish ejb

我对Java比较新。用服务器Tomcat尝试了我的第一个Web应用程序,相当不错。但是,使用另一台服务器Glassfish,会引起问题。在应用程序部署之后,jsp没有连接到servlet,因此出现错误404。我添加了项目结构和几个类。任何帮助表示赞赏。

empBean:

package app.bean;


import app.entity.Emp;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

@Stateless
public class EmpBean {
    @PersistenceContext(unitName = "DEVMODE")
    private EntityManager em;

    public Emp add(Emp emp) {
        return em.merge(emp);
    }

    public Emp get(Integer empno) {
        return em.find(Emp.class, empno);
    }

    public void update(Emp emp) {
        add(emp);
    }
    public void delete(long empno){
        em.remove(get((int) empno));
    }

    public List<Emp> getAll(){
        TypedQuery<Emp> namedQuery = em.createNamedQuery("Emp.getAll", Emp.class);
        return namedQuery.getResultList();
    }
}

的Emp:

package app.entity;


import javax.persistence.*;
import java.sql.Date;

@Entity(name="emp")
@NamedQuery(name = "Emp.getAll", query = "SELECT e from emp e")




public class Emp {
    @Id
    @Column(name="empno")
    private Integer empno ;
    @Column(name="ename")
    private String ename;
    @Column(name="job")
    private String job;
    @Column(name="mgr")
    private Integer mgr;
    @Column(name="hiredate")
    private Date hiredate;
    @Column(name="sal")
    private Integer sal;
    @Column(name="comm")
    private Integer comm;
    @Column(name="deptno")
    private Integer deptno;

    public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Integer sal, Integer comm, Integer deptno, String id) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
    }

    public Emp() {
    }

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Integer getMgr() {
        return mgr;
    }

    public void setMgr(Integer mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Integer getSal() {
        return sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    public Integer getComm() {
        return comm;
    }

    public void setComm(Integer comm) {
        this.comm = comm;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

}

Select1Servlet:

package app.servlets;

import app.bean.DeptBean;
import app.bean.EmpBean;
import app.bean.SalgradeBean;
import app.entity.Dept;
import app.entity.Emp;
import app.entity.Salgrade;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

@WebServlet("/search")
public class Select1Servlet extends HttpServlet {

    @EJB
    private EmpBean empBean;
    private DeptBean deptBean;
    private SalgradeBean salgradeBean;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        req.setCharacterEncoding("UTF-8");
        req.getRequestDispatcher("/searchview.jsp").forward(req, resp);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
//        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/users";
        String userName = "root";
        String password = "7777";


        Statement st;
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            conn = DriverManager.getConnection(url , userName, password);
            System.out.println("Connected!");
            String numb = request.getParameter("numb");

            ArrayList al = null;
            ArrayList pid_list = new ArrayList();

//            String query = "SELECT emp.ename, emp.job, emp.sal, dept.dname, salgrade.grade FROM dept,emp,salgrade WHERE (emp.empno = '" + numb + "' ) AND (emp.deptno = dept.deptno) and (emp.sal BETWEEN losal and hisal)  ";

//            System.out.println("query " + query);
            st = conn.createStatement();
//            ResultSet rs = st.executeQuery(query);
            Emp emp = empBean.get(Integer.valueOf(numb));
            String ename= emp.getEname();
            String job = emp.getJob();
            Integer sal = emp.getSal();
            Integer deptnoEmp= emp.getDeptno();
            Dept dept = deptBean.get(Integer.valueOf(deptnoEmp));
            String dname = dept.getDname();
            Salgrade salgrade = salgradeBean.get(sal);
            Integer grade = salgrade.getGrade();

//            while (rs.next()) {
                al = new ArrayList();
//                out.println(rs.getString(1));
//                out.println(rs.getString(2));
//                out.println(rs.getString(3));
//                out.println(rs.getString(4));
                al.add(ename);
                al.add(job);
                al.add(sal);
                al.add(dname);
                al.add(grade);

                System.out.println("al :: " + al);
                pid_list.add(al);
//            }

            request.setAttribute("piList", pid_list);
            RequestDispatcher view = request.getRequestDispatcher("/searchview.jsp");
            view.forward(request, response);
            conn.close();
            System.out.println("Disconnected!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

select1.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title> Enter the number </title>
    <link rel="stylesheet" href="design/w3.css">
</head>
<body>
<br/><br/>
<form method="post" name="frm" action="search">
    <table border="0" width="375" align="center" class = "w3-pink w3-text-white" >
        <tr><td colspan=2 style="font-size:12pt;" align="center">
            <h3>Search User</h3></td></tr>
        <br/>
        <tr><td ><b>User Number</b></td>
            <td>: <input type="number" name="numb" id="numb">
            </td></tr>
        <tr><td colspan=2 align="center">
            <br/>
            <input type="submit" name="submit" value="Search" onclick="form.action='/search';"></td></tr>
    </table>
</form>
</body>
</html>

searchview.jsp:
<%@ page import="java.util.*" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%--<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>--%>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title> List of emp </title>
    <link rel="stylesheet" href="design/w3.css">
</head>
<body>
<form  method="get" name="frm" action="searchview">
<table width="700px"
      class = "w3-text-pink">
    <tr>
        <td colspan=5 align="center"
           class = "w3-black w3-text-pink">
            <b>User Record</b></td>
    </tr>
    <tr class = "w3-black ">
        <td><b>User Name</b></td>
        <td><b>Job</b></td>
        <td><b>Sal</b></td>
        <td><b>Dname</b></td>
        <td><b>Grade</b></td>
    </tr>
    <%
       int count = 0;
       String color = "#ffffff";
       if (request.getAttribute("piList") != null) {
           ArrayList al = (ArrayList) request.getAttribute("piList");
           System.out.println(al);
           Iterator itr = al.iterator();
           while (itr.hasNext()) {

               if ((count % 2) == 0) {
                   color = "#ffffff";
               }
               count++;
               ArrayList pList = (ArrayList) itr.next();
   %>
    <tr style="background-color:<%=color%>;">
        <td><%=pList.get(0)%></td>
        <td><%=pList.get(1)%></td>
        <td><%=pList.get(2)%></td>
        <td><%=pList.get(3)%></td>
        <td><%=pList.get(4)%></td>
    </tr>
    <%
           }
       }
       if (count == 0) {
   %>
    <tr>
        <td colspan=5 align="center"
           style="background-color:#ffffff"><b>No Record Found..</b></td>
    </tr>
    <%            }
   %>
</table>
</form>
</body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
        <servlet>
            <servlet-name>Select1</servlet-name>
            <servlet-class>app.servlets.Select1Servlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>Select1</servlet-name>
            <url-pattern>/search</url-pattern>
        </servlet-mapping>

enter image description here

enter image description here

0 个答案:

没有答案