如何将链接重定向到servlet?

时间:2016-01-16 03:35:10

标签: java servlets

我有一个带有以下链接的html文件:

  <li><a class="active" href="PersonalInfoOutPut.java">View Personal Information</a></li>

当有人点击&#34;查看个人信息&#34;时,我想重定向到servlet,这就是我提出的原因:

href="PersonalInfoOutPut.java

但这不起作用。它正在回归:

HTTP Status 404 - /Payroll/PersonalInfoOutput.java

这是PersonalInfoOutput.java servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class PersonalInfoOutput extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(false);
        String employeeid = "";

        if (session != null) {
            employeeid = (String) session.getAttribute("employeeid");
        }

        boolean st = false;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
            PreparedStatement ps = con.prepareStatement(
                    "select employeeID,  FirstName, LastName, Admin, DOB, Address, Email, HourlyRate, Gender, ALeaveBalance, SLeaveBalance, ActiveStatus, Role, BSB, BankName, AccNumber, SuperNumber, SuperCompany from payroll_system.employee_info where employeeID = ?");
            ps.setString(1, employeeid);
            ResultSet rs = ps.executeQuery();
            st = rs.next();
            if (st) {
                boolean adminTrue = rs.getBoolean("Admin");
                boolean activeTrue = rs.getBoolean("ActiveStatus");

                out.println("<html>");
                out.println("<head>");
                out.println("<style>");
                out.println(
                        "table { border-collapse: collapse; width: 50%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even){background-color: #f2f2f2}");
                out.println("tr:hover {background-color: #e2f4ff;}");
                out.println("</style>");
                out.println("<link rel = stylesheet type = text/css href = main.css>");
                out.println("<title>Personal Information</title>");
                out.print("</head>");

                out.println("<body>");

                out.println("<h1>Personal Information</h1>");
                out.println("<table border =1>");

                out.println("<tr>");
                out.println("<td>Name</td>");
                out.println("<td>" + rs.getString("FirstName") + " " + rs.getString("LastName") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Employee ID</td>");
                out.println("<td>" + rs.getString("employeeID") + "</td>");
                out.println("</tr>");

                if (adminTrue) {
                    out.println("<tr>");
                    out.println("<td>Admin</td>");
                    out.println("<td>Yes</td>");
                    out.println("</tr>");
                } else {
                    out.println("<tr>");
                    out.println("<td>Admin</td>");
                    out.println("<td>No</td>");
                    out.println("</tr>");
                }

                out.println("<tr>");
                out.println("<td>Date Of Birth</td>");
                out.println("<td>" + rs.getString("DOB") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Residential Address</td>");
                out.println("<td>" + rs.getString("Address") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Email</td>");
                out.println("<td>" + rs.getString("Email") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Hourly Income</td>");
                out.println("<td>" + "$" + rs.getString("HourlyRate") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Gender</td>");
                out.println("<td>" + rs.getString("Gender") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Annual Leave Balance</td>");
                out.println("<td>" + rs.getString("ALeaveBalance") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Sick Leave Balance</td>");
                out.println("<td>" + rs.getString("SLeaveBalance") + "</td>");
                out.println("</tr>");

                if (activeTrue) {
                    out.println("<tr>");
                    out.println("<td>Currently Active</td>");
                    out.println("<td>Yes</td>");
                    out.println("</tr>");
                } else {
                    out.println("<tr>");
                    out.println("<td>Currently Active</td>");
                    out.println("<td>No</td>");
                    out.println("</tr>");
                }

                out.println("<tr>");
                out.println("<td>Role</td>");
                out.println("<td>" + rs.getString("Role") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>BSB</td>");
                out.println("<td>" + rs.getString("BSB") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Bank Name</td>");
                out.println("<td>" + rs.getString("BankName") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Bank Account Number</td>");
                out.println("<td>" + rs.getString("AccNumber") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Superannuation Company</td>");
                out.println("<td>" + rs.getString("SuperCompany") + "</td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>Superannuation Number</td>");
                out.println("<td>" + rs.getString("SuperNumber") + "</td>");
                out.println("</tr>");

                out.println("</table>");
                out.println("</body>");
                out.println("</html>");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.close();
    }
}

2 个答案:

答案 0 :(得分:0)

漫长而蜿蜒的道路等着你...... 首先,如果你想调用你的服务器,你应该在你的方法中有一些请求映射,例如:/personal/infooutput/,然后你需要将这个链接传递给你的html代码:

<li><a class="active" href="/personal/infooutput/">View Personal Information</a></li>

或调用servlet:

<li><a class="active" href="PersonalInfoOutPut">View Personal Information</a></li>

目前你没有HTTP GET方法,只有HTTP POST,你也需要添加它。 HTTPS状态404表示服务器未找到您的映射/链接。 在这里查看一些教程: http://tutorials.jenkov.com/java-servlets/index.html

答案 1 :(得分:0)

<a href="updateClient.rec" id="sample_editable_1_new" class="btn green">
      New Something <i class="fa fa-plus"></i>
</a>

updateClient.rec它是一个溃败的servlet,然后在你的Servlet类上:

String accion = request.getServletPath();
if ("/updateClient.rec".equals(accion)) {
            PrintWriter out = response.getWriter();
            request.getRequestDispatcher("solicitudWizard.jsp").forward(request, response);
            out.close();

} 如果你不想重定向到jsp,你可以重定向到同一个类Servlet上的方法,如下所示:

if ("/updateClient.rec".equals(accion)) {
            PrintWriter out = response.getWriter();
            String msg = this.getUpdateClint(request, response); //This redirect to a method of in the same class

private String getUpdateClint(HttpServletRequest request, HttpServletResponse response) {
    Integer pkClient = new Integer((String) request.getParameter("pkClient")); //Parameters of servlet
    //Other action 
    ClientPd cliPd = new ClientPd();
    String msg = null;
    msg = cliPd.getClient(pkClient);

    System.out.println(msg);
    return msg;
}

问候。