0

I am trying to create a register page which business logic is managed in servlet (Controller class), parameter values are called from a Javabean (Bean class) and uses a class to handle the database connectivity (Dbconnect class). We register and then we have to sign in through index.jsp. If the user is valid, the system should display a welcome page (welcome.jsp) showing the user name.

I don't know how to connect these classes to be called through my jsp to process the login. Here is the code showing what I have tried. I had seen many web source but couldn't understand the process..

This is how I think the communication of classes should be:

Dbconnection.java--->bean.java--->Controller.java--->register.jsp--->login.jsp--->welcome.jsp


Index.jsp:

User name:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" name="Login"> <a href="Register.jsp">Register</a>

Register.jsp

<form action="Loginservlet" method="post">
User name:<input type="text" name="name" >
Password:<input type="password" name="pass">
Email-id<input type="text" name="emailid">
phone number:<input type="text" name="phone">
<input type="submit" value="submit"><a href="index.jsp">Login</a>
</form>

Bean.java

package Databases;

public class Bean
{

    private   String username;
    private   String password;
    private   String emailid;
    private   String phone;
   /**
     * @return the username
     */
    public String getUsername()
    {
        return username;
    }
    /**
     * @param username the username to set
     */
    public void setUsername(String username)
    {
        this.username = username;
    }
    /**
     * @return the password
     */
    public String getPassword()
    {
        return password;
    }
    /**
     * @param password the password to set
     */
    public void setPassword(String password)
    {
        this.password = password;
    }
    /**
     * @return the emailid
     */
    public String getEmailid()
    {
        return emailid;
    }
    /**
     * @param emailid the emailid to set
     */
    public void setEmailid(String emailid)
    {
        this.emailid = emailid;
    }
    /**
     * @return the phone
     */
    public String getPhone()
    {
        return phone;
    }
    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone)
    {
        this.phone = phone;
    }

}

Dbconnect.java

package Databases;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

public class Dbconnect
{
    public static void main(String args[]) throws ClassNotFoundException
    {
        Dbconnect db = new Dbconnect();
        db.dbconnect();
    }

    public void dbconnect() throws ClassNotFoundException
    {

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from user");



            while(rs.next())
            {

                ArrayList al=new ArrayList();
                al.add(rs.getString("name"));
                al.add(rs.getString("password"));
                al.add(rs.getString("email"));

                Iterator it=al.iterator();

                while(it.hasNext())
                {


                    System.out.println("value are:"+it.next());

                }

            }

Controller.java:

package servlet;

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;

import com.mysql.jdbc.PreparedStatement;

import Databases.Dbconnect;

import Databases.*;

/**
 * Servlet implementation class Loginservlet
 */
public class Controller extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {

    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        String email = request.getParameter("emailid");
        String pn = request.getParameter("phone");
        System.out.println("Name :" + name);
        System.out.println("pass :" + pass);
        System.out.println("emailid :" + email);
        System.out.println("phone:" + pn);


        String a = Bean.setUsername(name);
        String b=Bean.setPassword(name);
        String c=Bean.setEmailid(name);
        String dBean.setPhone(name);


        String sql = "insert into user(name,password,email,phone) values(?,?,?,?)";

        Dbconnect a = new Dbconnect();
         a.dbconnect();

        PreparedStatement prep = a.dbconnect().prepareStatement(sql);

        // Setting the values which we got from JSP form

        prep.setString(1, a);
        prep.setString(2, b);
        prep.setString(3, c);
        prep.setString(4, d);
        prep.executeUpdate();
        prep.close();

    }

}

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WebprojectTry</display-name>
  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Controller</display-name>
    <servlet-name>Controller</servlet-name>
    <servlet-class>servlet.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/Controller</url-pattern>
  </servlet-mapping>
</web-app>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3331535
  • 1,127
  • 3
  • 13
  • 14
  • To voter to close: please at least ask about what's unclear before just voting. By the way, this is OP's question: *I don't know how to call all file in jsp to process so here is the code which [shows what] I have tried.* – Luiggi Mendoza Apr 04 '14 at 14:18
  • Where is your web.xml file?? this could mean alot because the servlet you are calling in your JSP needs to be Mapped Corectly. – Stanley Mungai Apr 04 '14 at 14:18
  • You should take some more time to write your question. Please reduce the code to a minimal example, and tell us your first problem, concisely. People aren't going to write your code for you, but if you have a specific technical problem you're encountering they'll be all over it. This is too broad. – John Humphreys Apr 04 '14 at 14:20
  • @w00te read my first comment... – Luiggi Mendoza Apr 04 '14 at 14:20
  • i updated post for web.xml – user3331535 Apr 04 '14 at 14:21
  • @LuiggiMendoza I wasn't the close voter, and I think my point is valid - I wasn't being mean. Teaching people to use the site better, spend some time on formatting, and spend time reducing their code to improve the quality of the question makes the site a better place for everyone. I used to post longer, less pointed questions and I've gotten better as a result of some people suggesting improvements. Nobody enjoys reading opening paragraphs without a break that ask you not to down-vote - the quality of the question should stop it from being downvoted. – John Humphreys Apr 04 '14 at 14:23
  • kindly try to understand my problems i am learning on the way java,servlet,jsp so now am trying to achieve by practically but i cant understand so if some one help on this ??? – user3331535 Apr 04 '14 at 14:28
  • 2
    @w00te and what I say is that the question is very specific. OP in the other hand has (kindly or naively) posted the necessary code and configurations to understand and replicate the problem (which sometimes lot of questions lack). The only improvement I could propose here would be removing `Bean` class code. The answer is simple: 1) There's no LoginServlet in this app, so the form will fail when being submitted. 2) `Controller` servlet never redirects nor forwards to another page, so even if it's called it will only output a blank page despite what it does, so OP has to change this. – Luiggi Mendoza Apr 04 '14 at 14:34
  • take a look at [this SO answer](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/3542297#3542297), you will get more patterns with mvc. – Abhishek Nayak Apr 05 '14 at 12:50
  • possible duplicate of [Servlet to jsp communication best practice](http://stackoverflow.com/questions/12252579/servlet-to-jsp-communication-best-practice) – Paul Sweatte Jun 19 '14 at 20:22

0 Answers0