Java web页面form调用servlet显示

阅读更多
Java web使用jsp页面的form表单调用servlet直接显示

1. 创建一个类LoginServlet继承HttpServlet类

LoginServlet.java

package com.andrew.servlet;
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;
public class LoginServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        System.out.println("doGet invoked!");
        process(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        System.out.println("doPost invoked!");
        process(req, resp);
    }
    private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println(req);
        System.out.println(resp);
        String username = req.getParameter("username1");
        String password = req.getParameter("password1");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("Login Result");
        out.println(" username: " + username + "
"); out.println("password: " + password + ""); out.flush(); } }


2. 配置web.xml文件

web.xml


    LoginServlet
    com.andrew.servlet.LoginServlet


    LoginServlet
    /LoginServlet



3.1 配置jsp页面,使用get方式调用servlet

login.jsp

username:
password:
   


url地址:
http://localhost:8080/test/LoginServlet?username1=hello&password1=world

控制台显示: doGet invoked!
org.apache.catalina.connector.RequestFacade@8634b6
org.apache.catalina.connector.ResponseFacade@eebce6

页面显示:
username: hello
password: world


3.2 配置jsp页面,使用post方式调用servlet

login.jsp

username:
password:
   


url地址: 
http://localhost:8080/test/LoginServlet

控制台显示: doPost invoked!
org.apache.catalina.connector.RequestFacade@8634b6
org.apache.catalina.connector.ResponseFacade@eebce6

页面显示:
username: hello
password: world

你可能感兴趣的:(Java,web)