Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner in jsp and servlets. I want to stay in the same page and populate the dropdownlist but iam not able to so. I did it using scriplets and it happened but scriplets are deprecated long back. I just want to know more about servlet mapping in web.xml. I guess something is wrong with the mapping. Please somebody help asap.

What I have tried:

Home.jsp
<form>
<select name="source" style="width:180 px"></select>
		
    <c:forEach items="${slist}" var="ls">
     <option> <c:out value="${ls}"/></option>
          
    </c:forEach>
</form>

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

	try{  
			Class.forName("com.mysql.cj.jdbc.Driver");  
			Connection con=DriverManager.getConnection(  
		"jdbc:mysql://localhost:3306/dbname","root","password");   
			Statement stmt=con.createStatement();  
			ResultSet rs=stmt.executeQuery("select * from City");  
			while(rs.next()) 
			{
			     i=rs.getInt(1);
			     val=rs.getString(2);
			     ar.add(val);
			    
			}
			 request.setAttribute("slist",ar);
			 
                  
                 request.getRequestDispatcher("/WebContent/Home.jsp").forward(request, response);
		     //rd.forward(req,res);
		     //System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  ");  
			con.close(); 
			}catch(Exception e){ System.out.println(e);}  
		finally
		{
			
		}
		
	}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900