简体   繁体   English

.JSP和servlet连接到.java类

[英].JSP and servlets connecting to .java class

I am having trouble trying to run my program. 我在尝试运行程序时遇到了麻烦。 It is supposed to get the information from the JSP page to the servlet, so that it can be passed through methods in a java class to be verified. 应该将信息从JSP页面获取到servlet,以便可以将其通过Java类中的方法传递以进行验证。 Then, once verified the servlet determines whehter to send th emessage to the previous JSP page or to the success JSP page. 然后,一旦验证,则servlet确定将消息发送到先前的JSP页面或成功的JSP页面。 When I run the program, it brings me to the RegisterUser.jsp page. 当我运行程序时,它将带我到RegisterUser.jsp页面。 I enter the information and then hit submit. 我输入信息,然后点击提交。 It brongs me to a 404 not found page. 它使我无法进入404未找到页面。 The link says it is trying topoint me to the CreateUser.java servlet. 该链接表示正在尝试将我指向CreateUser.java servlet。 It shouldn't be doing that. 它不应该那样做。 Any help would be greatly appreciated whether it is just a link to where I can find information or some actual solution help. 无论是仅仅是我可以在其中找到信息的链接还是实际的解决方案帮助,都将不胜感激。 Thank you. 谢谢。

  1. CreateUser.java SERVLET: CreateUser.java SERVLET:

     @WebServlet(name = "createUser", urlPatterns = {"/createUser"}) public class CreateUser extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param req * @param resp * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("in process login.java"); ServletContext context = getServletContext(); // Extract password and id from login.jsp form String userName = req.getParameter("USERNAME"); String password = req.getParameter("PASSWORD"); String answerOne = req.getParameter("ANSWERONE"); String answerTwo = req.getParameter("ANSWERTWO"); String answerThree = req.getParameter("ANSWERTHREE"); Boolean duplicateUser = User.verifyUserExists(userName); HttpSession session = req.getSession(); if (duplicateUser) { req.setAttribute("MESSAGE", "UserName is in use. Try again"); context.getRequestDispatcher("/RegisterUser.jsp").forward(req, resp); } else{ User user = new User(userName, password, answerOne, answerTwo, answerThree); user.addUser(user); req.getRequestDispatcher("RegistrationSuccessful.jsp").forward(req, resp); } } 

    } }

  2. RegisterUser.jsp RegisterUser.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>

    <body>
        <H1>New User Registration</H1>
        <H3>Required Information</H3>

        <FORM name="CREATE" action="servlets/CreateUser.java" method="POST" >
            <table>
                <tr>
                    <td>Username:</td>
                    <td><INPUT name="USERNAME" maxlength="50" size="40" type="text" ></TD>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><INPUT name="PASSWORD" maxlength="50" size="40" type="password" ></td>
                </tr>
                <tr>
                    <td>Favorite Vacation Spot:</td>
                    <td><INPUT name="ANSWERONE" maxlength="50" size="40" type="text" ></TD>
                </tr>
                <tr>
                    <td>Mother's Maiden Name:</td>
                    <td><INPUT name="ANSWERTWO" maxlength="50" size="40" type="text" ></TD>
                </tr>
                <tr>
                    <td>First Pet's Name:</td>
                    <td><INPUT name="ANSWERTHREE" maxlength="50" size="40" type="text" ></TD>
                </tr>
                <tr>
                    <td><input type="submit" name="Register" value="Register" onClick = "CreateUser.java"></td>
                    <td></td>
                </tr>
            </table>
        </FORM>
        <p>${MESSAGE}</p>

    </BODY>
</HTML>
  1. User.java class User.java类

public class User { 公共类用户{

private String userName;
private String password;
    private String answerOne;
    private String answerTwo;
    private String answerThree;
    private static File file = new File("C:\\Users.txt");

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public User(String un, String pw, String a1, String a2, String a3) {
        setUserName(un);
        setPassword(pw);
        setAnswerOne(a1);
        setAnswerTwo(a2);
        setAnswerThree(a3);

    }

    public void addUser(User user) throws IOException {
        FileWriter fw = new FileWriter(getFile());
        try (BufferedWriter bfw = new BufferedWriter(fw)) {
            bfw.write(user.userName);
            bfw.write(user.password);
            bfw.write(user.answerOne);
            bfw.write(user.answerTwo);
            bfw.write(user.answerThree);
            bfw.newLine();
        }

    }

    public static boolean verifyUserExists(String userName) throws FileNotFoundException {
        Scanner scannedFile = new Scanner(getFile());
        while (scannedFile.hasNext()) {
            String search = scannedFile.next();
            return search.equals(userName);
        }
        return false;
    }

    public static Boolean verifyPassword(String userName, String password) throws FileNotFoundException {
        Scanner scannedFile = new Scanner(getFile());
        while (scannedFile.hasNext()) {
            String search = scannedFile.next();
            if (search.equals(userName)) {
                return scannedFile.nextLine().equals(getMD5(password));
            }
        }
        return false;
    }

    public static String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            // Now we need to zero pad it if you actually want the full 32 chars.
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static File getFile() {
        return file;
    }

Use 采用

 action="createUser" OR action="./createUser" 

action="/createUser" will call the URL of default context action =“ / createUser”将调用默认上下文的URL

http://localhost:8084/createUser 

while action="./createUser" will call the URL pattern of the servlet in the current directory/context 而action =“ ./ createUser”将在当前目录/上下文中调用servlet的URL模式

 http://localhost:8084/ContextName/createUser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM