what is searching code in servlet to mysql?
I need simple coding anyone pls help me
i created a register page (Name,Qualification,Country,State) using with Html
I create a simple demo project which you mention, i use jstl:core
library which can be download from here
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
<display-name>TestServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>MyController</servlet-name>
<servlet-class>controller.MyController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyController</servlet-name>
<url-pattern>/MyController</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib
uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form
action="MyController"
method="post">
<input
type="text"
name="name"
value="yourUserName"> <input
type="submit"
value="Search">
</form>
<table title="Users List">
<c:forEach
items="${users}"
var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
MyController.java
package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.User;
public class MyController extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyController() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = null;
rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Get name request parameter
String name = request.getParameter("name");
// User user1 = new User();
// User user2 = new User();
// user1.setId(0);
// user1.setName("test1");
// user2.setId(0);
// user2.setName("test2");
List<User> users = new ArrayList<User>();
// users.add(user1);
// users.add(user2);
//DB Operation query your users
users=query(name);
request.setAttribute("users", users);
RequestDispatcher rd = null;
rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}
so MyController#query
method should be called from your service layer, and query according to your criteria(in demo name or can be anything)
EDIT: Be sure user
has get property methods, like getId
and getName
in this example.