servlet jsp web.xml实例

<body><!--action 不要写/./绝对路径 要写相当路径,这样便于维护->
<form action="LoginServlet" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password">

<input type="submit" value="submit">&nbsp;&nbsp;&nbsp
<input type="reset" value="reset">
</form>
</body>页面,method提供2种提交方式,get post一般使用post


在servlet获取表单数据

package server;

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 org.omg.CORBA.Request;

public class TestServlet extends HttpServlet{
  @Override
  protected void doGet(HttpServletRequest req,HttpServletResponse   
                       resp)throws ServletException,IOException{
    /*
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    out.println("<html><head><title>Hello World   
                Sample<title><head>");
    out.println("<body><h1>Hello<h1><body>");
    super.doGet(req,resq);
    */
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    resq.setContentType("text");
    PrintWriter out = resq.getWriter();
    out.println("<head><title>ok<title><head>");
    out.flush();
  }

    @Override
   protected void doPost(HttpServletRequest req,HttpServletResponse 
                        resp)throws ServletException,IOException{
     //TODO Auto-generated method stub
     super.doPost(req,resq);
   }  
}


web.xml

<!--name你的servlet类名字  class路径-->
<servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>com.serve.TestServlet</servlet-class>
</servlet>
<!--servlet-name名字 /所有的Loginservlet-->
<servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

表单提交之后,找web.xml 通过web.xml找到对应的servlet.jsp
转换字节码文件------>交给servlet--->编译成.class文件

你可能感兴趣的:(jsp,servlet,web.xml)