Servlet & JSP – 09 – Request – 02

Merhaba Arkadaslar,
Bu bolumde request konusuna devam edecegiz. ServletRequest ve HttpServletRequest arabirimlerinde bulunan metotlari incelemeye devam edecegiz

Ornek Uygulama

Bu ornek uygulamamizda yeni bir sinif olusturacagiz bir onceki paketi kullanabiliriz.

Sinifimiz ;
MoreRequestMethod

Deployment descriptor(web.xml) dosyamizda yeni servlet sinifimiz icin tanimlama yaplim.

	<servlet>
		<servlet-name>MoreRequestMethod</servlet-name>
		<servlet-class>_05_Request.MoreRequestMethod</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>MoreRequestMethod</servlet-name>
		<url-pattern>/morerequestmethod</url-pattern>
	</servlet-mapping>

MoreRequestMethod.java

package _05_Request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MoreRequestMethod extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		PrintWriter pw = resp.getWriter();

		String hostHeader = req.getHeader("host");
		String serverName = req.getServerName();
		int localPort = req.getLocalPort();
		int serverPort = req.getServerPort();
		int remotePort = req.getRemotePort();
		String method = req.getMethod();
		String contexPath = req.getContextPath();

		pw.println("<html><body>");
		pw.println("<table>");
		pw.println("<tr><td>host<td>" + hostHeader);
		Enumeration<String> headerNames = req.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String headerName = headerNames.nextElement();
			pw.println("<tr><td>" + headerName);
			pw.println("<td>" + req.getHeader(headerName));
		}

		pw.println("<tr><td>serverName<td>" + serverName);
		pw.println("<tr><td>localPort<td>" + localPort);
		pw.println("<tr><td>serverPort<td>" + serverPort);
		pw.println("<tr><td>remotePort<td>" + remotePort);
		pw.println("<tr><td>method<td>" + method);
		pw.println("<tr><td>contexPath<td>" + contexPath);
		pw.println("</table>");
		pw.println("</html></body>");
	}
}

Sinifimizi calistiralim ve sonuclar uzerinden inceleyelim.

headers ports

public String getHeader(String name);

getHeader() metodu ile header bilgilerine ulasabiliriz.

public Enumeration<String> getHeaderNames();

getHeaderNames() metodu tum header bilgilerini geri dondururur.

public String getServerName();

getServerName() metodu server ismini dondurur; localhost.

public int getLocalPort();
public int getServerPort();

local port ve server port arasinda kucuk bir kavram farkliligi vardir.
server port requestin ilk karsilandigi basladigi porttur. local port ise bu istegin tamamlandigi porttur.

public int getRemotePort();

Istegin gonderildigi porttur. Remote’tan kasit client/istemci yani tarayicidir.

public String getMethod();

getMethod() , bize kullandigimiz HTTP metodunun tur bilgisini dondurur.

public String getContextPath();

getContextPath() metodu context path bilgisini dondurecektir.

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6, Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *