1.servletResponse:
1)getWriter():返回printWriter对象,直接打印到控制台上。
PrintWriter out=response.getWriter();
out.print();
2)setContentType():设置相应类型。apption/msword
3)void sendRedirect(String location):请求重定向,为httpServletResponse中定义。
2.为精简程序新建一个
1)MyGenericServlet 类即 GenericSerlet 类:
public abstract class MyGenericServlet implements Servlet,ServletConfig {
private ServletConfig servletConfig;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return servletConfig;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
this.servletConfig=arg0;
}
@Override
public abstract void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException ;
@Override
public String getInitParameter(String arg0) {
return servletConfig.getInitParameter(arg0);
}
@Override
public Enumeration<String> getInitParameterNames() {
return servletConfig.getInitParameterNames();
}
@Override
public ServletContext getServletContext() {
return servletConfig.getServletContext();
}
@Override
public String getServletName() {
// TODO Auto-generated method stub
return servletConfig.getServletName();
}
}
2)MyHttpServlet类 即 HttpServlet 类:
public class MyHttpServlet extends MyGenericServlet{
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
if(arg0 instanceof HttpServletRequest){
HttpServletRequest request=(HttpServletRequest) arg0;
if(arg1 instanceof HttpServletResponse){
HttpServletRequest response=(HttpServletRequest) arg1;
service(request,response);
}
}
}
private void service(HttpServletRequest request, HttpServletRequest response)
throws ServletException,IOException{
String method=request.getMethod();
if("GET".equalsIgnoreCase(method)){
doGET(request,response);
}else
if("POST".equalsIgnoreCase(method)){
doPOST(request,response);
}
}
public void doPOST(HttpServletRequest request, HttpServletRequest response)
throws ServletException,IOException{
}
public void doGET(HttpServletRequest request, HttpServletRequest response)
throws ServletException,IOException{
}
}
3)继承Httpservlet接口实现数据库连接:
public class loginServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("text");
String pass = req.getParameter("password");
Connection con = null;
PreparedStatement pre = null;
ResultSet res = null;
PrintWriter out = resp.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:///test";
con = DriverManager.getConnection(url, "root", "root");
String sql = "select count(id) from test_user where user=? and password=?";
pre = con.prepareStatement(sql);
pre.setString(1, user);
pre.setString(2, pass);
res = pre.executeQuery();
while (res.next()) {
int count = res.getInt(1);
if (count > 0) {
out.print("hello");
} else {
out.print("sorry");
}
}
}
1)getWriter():返回printWriter对象,直接打印到控制台上。
PrintWriter out=response.getWriter();
out.print();
2)setContentType():设置相应类型。apption/msword
3)void sendRedirect(String location):请求重定向,为httpServletResponse中定义。
2.为精简程序新建一个
1)MyGenericServlet 类即 GenericSerlet 类:
public abstract class MyGenericServlet implements Servlet,ServletConfig {
private ServletConfig servletConfig;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return servletConfig;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
this.servletConfig=arg0;
}
@Override
public abstract void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException ;
@Override
public String getInitParameter(String arg0) {
return servletConfig.getInitParameter(arg0);
}
@Override
public Enumeration<String> getInitParameterNames() {
return servletConfig.getInitParameterNames();
}
@Override
public ServletContext getServletContext() {
return servletConfig.getServletContext();
}
@Override
public String getServletName() {
// TODO Auto-generated method stub
return servletConfig.getServletName();
}
}
2)MyHttpServlet类 即 HttpServlet 类:
public class MyHttpServlet extends MyGenericServlet{
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
if(arg0 instanceof HttpServletRequest){
HttpServletRequest request=(HttpServletRequest) arg0;
if(arg1 instanceof HttpServletResponse){
HttpServletRequest response=(HttpServletRequest) arg1;
service(request,response);
}
}
}
private void service(HttpServletRequest request, HttpServletRequest response)
throws ServletException,IOException{
String method=request.getMethod();
if("GET".equalsIgnoreCase(method)){
doGET(request,response);
}else
if("POST".equalsIgnoreCase(method)){
doPOST(request,response);
}
}
public void doPOST(HttpServletRequest request, HttpServletRequest response)
throws ServletException,IOException{
}
public void doGET(HttpServletRequest request, HttpServletRequest response)
throws ServletException,IOException{
}
}
3)继承Httpservlet接口实现数据库连接:
public class loginServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("text");
String pass = req.getParameter("password");
Connection con = null;
PreparedStatement pre = null;
ResultSet res = null;
PrintWriter out = resp.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:///test";
con = DriverManager.getConnection(url, "root", "root");
String sql = "select count(id) from test_user where user=? and password=?";
pre = con.prepareStatement(sql);
pre.setString(1, user);
pre.setString(2, pass);
res = pre.executeQuery();
while (res.next()) {
int count = res.getInt(1);
if (count > 0) {
out.print("hello");
} else {
out.print("sorry");
}
}
}