开发者

How to store usernames and passwords in a very simply registration form using servlet/jsp/javabeans?

I understand how to use session cookies to pass a username and password from servlet to servlet but I am baffled on how to store successful registrations so that when someone new registers it checks it against that data store to make sure the username they choose has not already been used.

Keep in mind, none of this has to be secure in any way, it just has to work.

I have a form setup that asks for username, password, and confirm password setup and passes the form da开发者_StackOverflow社区ta to a servlet named process.

For the life of me I cannot figure out a simple way to make this work without getting into more advanced techniques that require databases or other means.

Here is the code for my form. Any thing that can point me in the right direction would be of great help. Thank you.

package HWpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class register extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ///* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet register</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<form action='process' method='post'>");
            out.println("<table border ='5'>" +
                        "<tr>" +
                        "<th colspan='2'>Register Account Information</th>" +
                        "</tr>" +
                        "<tr>" +
                        "<td>Requested Username:</td>" +
                        "<td><input type='text' name='username' /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td>Password:</td>" +
                        "<td><input type='password' name='password' /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td>Confirm Password:</td>" +
                        "<td><input type='password' name='password' /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<th colspan='2'><input type='Submit' value='Submit'><input type='Reset' value='Reset' /></form></th>" +
                        "</tr>" +
                        "</table>");
            out.println("</body>");
            out.println("</html>");
            //*/
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}


I have few suggestions for you.

  • You are mixing view with controller. Don't do this , follow MVC model
  • Yes you are right, username/password should get stored in DB only, use JDBC to query your DB.
  • See Servlet


Try db4o. It can save your objects in just one line of code and just one jar in your classpath.

db.store(user);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜