无法做国家,城市,州使用Servlets,JSP,MySql下载

时间:2018-05-31 12:58:05

标签: java mysql jsp servlets

我正在尝试从mysql db填充国家/地区,州和城市,并将它们显示在home.jsp中作为预览,但我遇到了一些错误。

实际上我正在尝试用户注册表单

我有分配了外键和主键的国家/地区,州,城市表 我有3个文件index.jsp,RegistrationController.java,home.jsp用于显示

我只想知道如何在下面的场景中正确地下拉链接国家,城市,州 任何帮助和建议将不胜感激。

index.jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="country-state-dropdown.js"></script>
<title>Registration</title>
<style>
th {
text-align: left
}
</style>
</head>
<body>
<fieldset >
<legend>Registration Form</legend>
<div class="ex">
<form action="RegistrationController" method="post"><!-- action="RegistrationController"  -->
<table border="1" align="center" width="50%" cellpadding="5" cellspacing="6">

<tr>
<th>First Name</th>
<td><input type="text" width="auto" name="fn" ></td>
</tr>
<tr>
<th>Last Name</th>
<td><input type="text" name="ln"></td>
</tr>
<tr>
<th>Email</th>
<td><input type="email" name="e" ></td>
</tr>
<tr>
<th>Phone</th>
<td><input type="tel" name="p"></td>
</tr>

<tr>
<th>Gender</th>
<td>
<input type="radio" name="gender" id="male" value="Male">Male
<input type="radio" name="gender" id="female" value="Female">Female
</td>
</tr>

<tr>
<th>Address</th>
<td>
<textarea name="address"></textarea>
</td>
</tr>

 <!-- <tr>
<th>Country</th>
<td>
<select name="country" id="countrySel" size="1">
<option value="" selected="selected">-- Select Country --</option>
</select>
</td>
</tr>
<tr>
<th>State</th>
<td>
<select name="state" id="stateSel" size="1">
<option value="" selected="selected">-- Select State --</option>
</select>
</td>
</tr>
<tr>
<th>City</th>
<td>
<select name="city"id="citySel" size="1">
<option value="" selected="selected">-- Select City --</option>
</select>
</td>
</tr>
<tr>
<th>Zip Code</th>
<td>
<select name="zip" id="zipSel" size="1">
<option value="" selected="selected">-- Select Zip --</option>
</select>
</td>
</tr> -->
<tr>
<th>Country</th>
<td>
<select name="country" id="country" onchange="this.form.submit();">
<option value="0">Select Country</option>
<%

try{
    String Query="select * from country";
    Class.forName("com.mysql.jdbc.Driver");  
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chiru","root","admin123");
    Statement st = con.createStatement();
    ResultSet rs=st.executeQuery(Query);
    while(rs.next()){
        %>
        <option value="<%=rs.getInt("country_id")%>"
        <%
        if(request.getParameter("country")!=null)
        {
            if(rs.getInt("country_id")==Integer.parseInt(request.getParameter("country"))){
                out.print("selected");
            }
        }
        %>      
        ><%=rs.getString("country_name")%></option>
        <%
    }
}catch (Exception ex){
    ex.printStackTrace();
}
%>
</select>
</td>
</tr> 

 <tr>
<th >State</th>
<td>
<select name="state" id="state">
<option value="0">Select State</option>
<%
try{
    String Query = "select * from state where country_id=?";
     Class.forName("com.mysql.jdbc.Driver");    
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/chiru","root","admin123");
    PreparedStatement pst = con.prepareStatement(Query);
    pst.setString(1, request.getParameter("country"));
    ResultSet rs = pst.executeQuery();
    while(rs.next()){
        %>
        <option value="<%=rs.getInt("state_id")%>"><%=rs.getString("state_name")%></option>
        <%
    }
}catch (Exception ex){
    ex.printStackTrace();
}
%>
</select>
</td>
</tr> 


<tr>
<th>Status</th>
<td>
<input type="radio" name="status" id="Active" value="Active">Active
<input type="radio" name="status" id="InActive" value="InActive">InActive<br>
</td>
</tr>

</table><br>
<input type="submit" value="Register" style=" margin-left: 45%; padding: 10px; width: auto; background-color:Crimson; color:Cornsilk" />
</form>
</div>
</fieldset>

</body>
</html>

RegistrationController.java代码

package com.registration;
import java.io.IOException;
import java.io.PrintWriter;

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;
@WebServlet("/RegistrationController")
public class RegistrationController extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String fname = req.getParameter("fn");
        String lname = req.getParameter("ln");
        String email = req.getParameter("e");
        String phone = req.getParameter("p");
        String sex = req.getParameter("gender");
        String address= req.getParameter("address");
        String status = req.getParameter("status");
        String country = req.getParameter("country");
        String state = req.getParameter("state");
        /*String city = req.getParameter("city");
        String zipcode = req.getParameter("zip");*/
        if(fname.isEmpty()||lname.isEmpty()||email.isEmpty() || 
                phone.isEmpty()||status.isEmpty()||address.isEmpty()|| country.isEmpty()||state.isEmpty()) //||city.isEmpty()||zipcode.isEmpty()
        {
            RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
            rd.include(req, res);
            /*res.sendRedirect("index.jsp");*/
        }
        else
        {
            RequestDispatcher rd = req.getRequestDispatcher("home.jsp");
            rd.forward(req, res);
            /*res.sendRedirect("home.jsp");*/
        }

//      if(req.getParameter("registrationsuccess").equals("Submit")) {
//          RequestDispatcher rd = req.getRequestDispatcher("home.jsp");
//          req.setAttribute("message", "Your registration completed successfully !!");
//          rd.forward(req, res);
//      }



    }

}

home.jsp进行预览

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
table#nat {
    width: 50%;
    background-color: powderblue ;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Preview</title>
</head>
<body>
<%
String fname = request.getParameter("fn");
String lname = request.getParameter("ln");
String email = request.getParameter("e");
String phone = request.getParameter("p");
String sex = request.getParameter("gender");
String address = request.getParameter("address");
String status = request.getParameter("status");
String country = request.getParameter("country");
String state = request.getParameter("state");
/* String city = request.getParameter("city");
String zipcode = request.getParameter("zip"); */
%>
<fieldset>
<legend>Preview of User Data</legend>
<table id="nat" border="1" align="center" width="50%" cellpadding="5" cellspacing="6">

<tr>
<td>First Name</td>
<td><%= fname %></td>
</tr>
<tr>
<td>Last Name</td>
<td><%= lname %></td>
</tr>

<tr>
<td>Email</td>
<td><%= email %></td>
</tr>

<tr>
<td>Phone</td>
<td><%= phone %></td>
</tr>

<tr>
<td>Gender</td>
<td><%= sex%></td>
</tr>

<tr>
<td>Status</td>
<td><%= status%></td>
</tr>
<tr>
<td>Address</td>
<td><%= address %></td>
</tr>
 <tr>
<td>Country</td>
<td><%= country%></td>
</tr>
<tr>
<td>State</td>
<td><%= state%></td>
</tr> 
<%-- <tr>
<td>City</td>
<td><%= city%></td>
</tr> 
<tr>
<td>Zip Code</td>
<td><%= zipcode%></td>
</tr>  --%>
<tr>
<td><form action="index.jsp"><input type="submit" value="Go Back" style=" margin-top:18px; padding:10px; color: powderblue; background-color:Green"></form><br></td>
<td><form action="RegistrationSuccess.jsp"><input type= "submit" value="Submit" style= " padding:10px; color: powderblue; background-color:Green"></form></td>
</tr>

<!-- <tr>
<td>

</td>
</tr> -->
</table>

0 个答案:

没有答案